Pick image from fragment always return resultcode 0 in some devices
Asked Answered
N

1

1

I trying to pick an image from gallery and set the bitmap to my imageview, but I have a problem: in my device, works well, but it doesn't work in other.

I start the image picker in my fragment as follow:

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, SELECT_PHOTO);

And this is my onActivityResult:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 

         super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

         Log.d("ResultCode",resultCode+"");

         switch (requestCode) { 

               case SELECT_PHOTO:
                     if (resultCode == Activity.RESULT_OK) { 
                             Uri selectedImage = imageReturnedIntent.getData(); 
                             try { 
                                    Bitmap imagen = decodeUri(selectedImage);

                                     // Works on my device (because resultCode = RESULT_OK) but doesn't work in others (because resultCode = 0)
                                    ((ImageView) findViewById(R.id.myimage)).setImageBitmap(imagen); 

                             } catch (FileNotFoundException e) { 
                                    e.printStackTrace(); 
                             } 
                     } 
           } 
}

I would appreciate any help, I'm a little desperate. x_x

Nordstrom answered 23/9, 2015 at 16:54 Comment(1)
Any suggestion? I dont know whats happen...Nordstrom
L
5

I have had this issue... so much fun. There are three different ways to choose photos, and like you said, for some reason it doesn't always work properly on all devices. After many hours of torture, I found this worked consistently:

 Intent intent = new Intent();
 intent.setAction(Intent.ACTION_GET_CONTENT);
 intent.setType("image/*");
 startActivityForResult(Intent.createChooser(intent,"whatever you want",1);

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode)
    {

       case 1:

            if (resultCode == RESULT_OK)
            {
                Uri selectedImage = data.getData();
            }
            break;
    }
 }
Lesbian answered 23/9, 2015 at 18:52 Comment(4)
Hi Alexander, doesnt work :( I still get resultcode 0. Where do you put those codes? Inside a fragment? Please, if you can, help me T_TNordstrom
Oh, works well! I had singleinstance in my activity. I remove that and works :))Nordstrom
SingleInstance - that was it! I was scratching my head over it.Pelagi
I've set singleTop instead of singleInstance. WorkedDisposed

© 2022 - 2024 — McMap. All rights reserved.