Pick image from fragment always return resultcode

2019-09-10 02:55发布

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

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-10 03:12

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;
    }
 }
查看更多
登录 后发表回答