Android get REQUEST_IMAGE_CAPTURE extras with Glide - android

I'm trying to use Glide library to manage the Bitmap captured by the camera.
Here the working code (without Glide)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
try{
Bundle extras = data.getExtras();
Bitmap photo = (Bitmap) extras.get("data");
imageView.setImageBitmap(photo);
}
catch(Exception e){
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
And here my try with Glide:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
try{
Bundle extras = data.getExtras();
Bitmap photo = (Bitmap) extras.get("data");
//imageView.setImageBitmap(photo);
Glide.with(imageView.getContext()).load(extras.get("data")).asBitmap().override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).fitCenter().into(imageView);
}
catch(Exception e){
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
My imageView remains empty.

A good workaround by using the real image:
else if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
Uri uri = data.getData();
try{
imageView.setImageDrawable(null);
//Bundle extras = data.getExtras();
//Bitmap photo = (Bitmap) data.getExtras().get("data");
//imageView.setImageBitmap(photo);
//InputStream inputStream = getContentResolver().openInputStream(u);
//BitmapFactory.Options options=new BitmapFactory.Options();
//options.inSampleSize=3; //decrease decoded image
//Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
//imageView.setImageBitmap(bitmap);
Glide.with(imageView.getContext()).load(uri).asBitmap().override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).fitCenter().into(imageView);
//extras.clear();
}
catch(Exception e){
e.printStackTrace();
}
}

Related

How to get image from camera

I have tried many codes from stackoverflow and internet.But I am able to find how to pick image from camera.I have used the following code,but data.getData() always returns null.Dont know how to solve.Pleas help this.
Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, PICK_IMAGE_CAMERA1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 30 && resultCode == Activity.RESULT_OK&&data!null)
{
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}
Try this code #Thrishool,
private static final int CAMERA = 1;
//choosing the image from camera
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
//now get the data from the onActivity result
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imageview.setImageBitmap(thumbnail);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp = Base64.encodeToString(b, Base64.DEFAULT);
Log.e("savedImage",temp);
Toast.makeText(ProfileActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
}
}
Try this and let me know #Thrishool

Selecting image from a gallery app

I was using this code to allow the user to choose an image from a gallery app and to get that image afterwards.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == getActivity().RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
mImg.setImageBitmap(photo);
} else if (requestCode == SELECT_FILE) {
Uri selectedImageUri = data.getData();
String[] projection = {MediaStore.MediaColumns.DATA};
CursorLoader cursorLoader = new CursorLoader(getActivity(), selectedImageUri, projection, null, null,
null);
Cursor cursor = cursorLoader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
Bitmap bm;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
final int REQUIRED_SIZE = 200;
int scale = 1;
while (options.outWidth / scale / 2 >= REQUIRED_SIZE
&& options.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(selectedImagePath, options);
mImg.setImageBitmap(bm);
mImg.setAlpha(1);
}
}
}
But in this answer here I was told that this code wont work in most android devices. Can I know why? And what is the best way to get an image chosen by the user.
I posted this in a separate question because it might be interesting.
By using bitmap, your image may looked blurry. If the image size too large, it will be a problem when you want to upload or retrieved them to(from) server. So uri is better than bitmap.
You may try below code and you will see the difference between bitmap and uri
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
selectedImage = data.getData();
imageView.setImageURI(selectedImage);
}
else
{
imageView.setImageResource(R.mipmap.no_image);
}
break;
case REQUEST_IMAGE_CAPTURE:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
imageView.setImageURI(null);
imageView.setImageURI(imageUri);
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
Log.e("A", "AAA");
}
}
}

OnActivityResult doesn't update ImageView using Picasso

Using following code to view captured image in an ImageView
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(this.requestCode == requestCode && resultCode == RESULT_OK) {
File imageFile = new File(Environment.getExternalStorageDirectory() + "/img.jpeg");
Picasso.with(MainActivity.this).load(imageFile).into(imageView);
}
}
Problem:
How Can I refresh ImageView in onActivityResult(...) as you can see I am using above code but always getting preview of old image, whereas i am getting new image inside storage
You can replace the code these lines of code for it:-
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(this.requestCode == requestCode && resultCode == RESULT_OK) {
File imageFile = new File(Environment.getExternalStorageDirectory() + "/img.jpeg");
Picasso.with(MainActivity.this)
.load(imageFile)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.error(R.drawable.ic_lanucher)
.noFade()
.into(imageView)
}
}
If you want to load the captured image, you need to get bitmap from data i.e. your Intent object.
if (responseCode == RESULT_OK && data != null) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
if (bitmap != null) {
// Here you will get bitmap , set bitmap in imageview
}
}

How to display an image that the user just took with camera

I am trying to give the user the option to either choose an image from the phone's storage or take one with the phone's camera app and then display that image in an imageView. With the help of this and this question I am able to display the image when it is choosen from the storage. However if I choose to take a picture with camera although I don't get an error the image is not shown in the imageView.
The function for choosing the picture looks like:
public void choosePic(View view) {
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
chooserIntent.putExtra
(
Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent }
);
startActivityForResult(chooserIntent, SELECT_PICTURE);
}
After that it is handled by
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
image = (ImageView) findViewById(R.id.imageView);
image.setImageURI(_uri);
cursor.close();
}
super.onActivityResult(requestCode, resultCode, data);
}
EDIT
With #Murtaza Hussain's helps I was able to find a working solution:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("requestCode",""+requestCode);
if(requestCode == PICK_IMAGE && resultCode==RESULT_OK && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
image = (ImageView) findViewById(R.id.imageView);
image.setImageURI(_uri);
cursor.close();
}
else if(requestCode== PICK_IMAGE && resultCode==RESULT_OK && data.getData() == null){
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
image =(ImageView) findViewById(R.id.imageView);
image.setImageBitmap(thePic);
}
super.onActivityResult(requestCode, resultCode, data);
}
You need to add code for it in onActivityResult()
if(requestCode==0 && resultCode==RESULT_OK ){
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
image =(ImageView) findViewById(R.id.imageView);
image.setImageBitmap(thePic);
}
In your activity when to capture image from camera. write the below code.
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"/imageLarge"+System.currentTimeMillis()+".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, 111);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
In onActivityResult() write the below code..
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 111:
if(resultCode == RESULT_OK)
{
Log.v("","After camera capture path is --> "+mImageCaptureUri.toString().substring(8));
Bitmap bitmap=BitmapFactory.decodeFile(mImageCaptureUri.toString().substring(8));
image.setImageBitmap(bitmap);
}
else
{
}
}
}
Do not forget to add one permission.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
In case of any query, Please let me know. Thanks.

Android camera intent losing reference of Imageview in Lollipop

I have an activity in which i am calling intent of camera on click, but when I capture image and get back to the activity result my Image view reference is null. I am getting URI correctly. This happens in android Lollipop.
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
//retrieve a reference to the ImageView
Log.i("dataa",String.valueOf(thePic));
Log.i("dataaimg",String.valueOf(image));
Log.i("dataaimgno",String.valueOf(imageno));
image.setImageBitmap(thePic); // here is the problem image reference is null when get back here
}
}
This code is working fine in versions below lollipop.
Try this:
private static final int CAPTURE_IMAGE_ACTIVITY_REQ = 0;
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ );
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQ) {
if (resultCode == RESULT_OK) {
Bitmap bp = (Bitmap) data.getExtras().get("data");
image = setImageBitmap(bp);
}
}

Categories

Resources