OnActivityResult doesn't update ImageView using Picasso - android

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
}
}

Related

Uri for onActivityResult is always null

I want to take a picture with the camera and then put it in the Firebase (Realtime Database) as a URI. Unfortunately, I always get a null value as Uri. How do I have to proceed so that this is not zero?
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent cam = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cam,CAMERA);
}else{...}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA && resultCode == RESULT_OK && data != null)
{
Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
vorschau.setImageBitmap(bitmap);
}
}

How to set Image Button to some image in my internal storage (Android)?

Help me to set the uri of the image button to the image that i select form the internal storage. I have already given required permissions in the android_manifest.xml file. The problem is that when i browse to the internal storage of phone the images are unselectable.
public void onUploadImageClick(View view){
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("Image/*");
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageURI(uri);
}
}
public void onUploadImageClick(View view){
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
Then in your on activity result method keep this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageURI(uri);
}
}
EDIT
yes the problem is I forgot to add .Media
so use this line of code:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
You can try below code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
String pathName = uri.toString();
Bitmap bmp = BitmapFactory.decodeFile(pathName);
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageBitmap(bmp);
}
}
Can you try this. You could replace ImageView to ImageButton
String qrPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Image.png";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap mustOpen = BitmapFactory.decodeFile(qrPath, options);
setContentView(R.layout.qr_image);
ImageView imageView = (ImageView) findViewById(R.id.qr_image);
imageView.setImageBitmap(mustOpen);

Android get REQUEST_IMAGE_CAPTURE extras with Glide

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();
}
}

Get full quality image from camera

When I take a picture with the camera and then I want to show this image in a ImageView, I was following the next method:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {
Bitmap srcBmp = (Bitmap) data.getExtras().get("data");
... (process image to scale size and rotate if necesary)
pic_view.setImageBitmap(srcBmp);
}
}
}
I was getting the image and showing it in the ImageView correctly, but I have realized that the image obtained was of very low quality. After some research I found that the image obtained with this method is the thumbnail of the image taken. So I modified the code following some indications from other SO posts talking about this:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "Pictures/timeStamp.jpg";
takenPicUri = Uri.fromFile(new File(imageFilePath));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, takenPicUri);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {
Bitmap srcBmp = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, null);
... (process image to scale size and rotate if necesary)
pic_view.setImageBitmap(srcBmp);
}
}
}
But now, the image is not being shown in the ImageView(pic_view). In the other posts I have read people report that this method worked for them, but is not working for me. I'm forgetting something or I'm doing something wrong?
Well, I have read in old posts that this way of doing this could cause trouble because there was some bug related to it but is the only way in that I have achieved to make it work.
Is as simple as this:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQUEST_FRAG);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_IMAGE_REQUEST_FRAG:
if (resultCode == getActivity().RESULT_OK && data != null) {
Uri selectedImageUri = data.getData();
Bitmap srcBmp = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, null);
... (process image to scale size and rotate if necesary)
pic_view.setImageBitmap(srcBmp);
}
}
}
This way is working on android 5.0 and 4.4.4.

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