从片段中选择图像始终在某些设备中返回结果码0

Mar*_*Gom 0 android android-intent android-gallery android-fragments onactivityresult

我试图从库中选择一个图像并将位图设置为我的imageview,但我有一个问题:在我的设备中,运行良好,但它在其他方面不起作用.

我在片段中启动图像选择器如下:

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

这是我的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(); 
                             } 
                     } 
           } 
}
Run Code Online (Sandbox Code Playgroud)

我很感激任何帮助,我有点绝望.X_X

A.S*_*.SD 5

我有这个问题......非常有趣.有三种不同的方式可以选择照片,就像你说的那样,由于某种原因,它并不总能在所有设备上正常工作.经过几个小时的折磨,我发现这一点始终如一:

 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;
    }
 }
Run Code Online (Sandbox Code Playgroud)

  • 哦,效果很好!我的活动中有单身实例.我删除并工作:)) (3认同)