从这里选择的片段影像总是返回结果code 0在某些设备 [英] Pick image from fragment always return resultcode 0 in some devices

查看:166
本文介绍了从这里选择的片段影像总是返回结果code 0在某些设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拿起从画廊的图像和位图设置为我的ImageView,但我有一个问题:在我的设备,效果很好,但它并没有在其他工作

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:

意图photoPickerIntent =新意图(Intent.ACTION_PICK); photoPickerIntent.setType(图像/ *);
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(); 
                             } 
                     } 
           } 
}

我想AP preciate任何帮助,我有点绝望。 X_X

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

推荐答案

我有这个问题......这么多的乐趣。有三种不同的方法来选择照片,还有像你说的,由于某种原因,它并不总是正确地在所有设备上工作。几个小时的折磨后,我发现这工作始终坚持:

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

这篇关于从这里选择的片段影像总是返回结果code 0在某些设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆