Pick image from fragment always return resultcode 0 in some devices

╄→гoц情女王★ 提交于 2019-12-25 05:50:06

问题


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:


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


来源:https://stackoverflow.com/questions/32745250/pick-image-from-fragment-always-return-resultcode-0-in-some-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!