【问题标题】:Android pick image from gallery not working (Android 6.0 & Android 5.1.1)Android 从图库中选择图片不起作用(Android 6.0 和 Android 5.1.1)
【发布时间】:2016-10-03 13:06:55
【问题描述】:

我正在使用下面的代码从设备库中选择一个图像文件:

首先我将这段代码称为:

Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(i, "Select Picture"), RESULT_LOAD_IMAGE);

这是我的onActivityResult 方法:

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

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {

            photoPath  = getPath(data.getData());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            FileInputStream fis;
            try {
                fis = new FileInputStream(new File(photoPath));
                byte[] buf = new byte[1024];
                int n;
                while (-1 != (n = fis.read(buf))) {
                    baos.write(buf, 0, n);
                }

                img.setImageBitmap(BitmapFactory.decodeFile(photoPath));

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

        }
    }

这是一个检索图像路径的辅助方法:

private String getPath(Uri uri) {

        String[]  data = { MediaStore.Images.Media.DATA };
        CursorLoader loader = new CursorLoader(getApplicationContext(), uri, data, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

我的问题是应用程序很奇怪。在我的 6.0 模拟器中,有时它可以工作,有时不能。在其他设备(Android 5.1.1)中,FileNotFound Exception 会在此行抛出

fis = new FileInputStream(new File(photoPath));

所有必需的权限都很好。你们知道这里发生了什么吗?或者你有什么更好的建议来从画廊中挑选图像?

【问题讨论】:

    标签: android image action gallery photo-gallery


    【解决方案1】:

    第 1 步:删除 ByteArrayOutputStream,因为您没有使用它。

    第 2 步:删除 photoPath = getPath(data.getData());getPath() 方法,因为它们是错误的。

    第 3 步:使用an image loading library 异步填充您的ImageView,将要加载的图像的Uri (data.getData()) 传递给它。或者,滚动大量您自己的图像加载代码,包括分叉一个后台线程并使用getContentResolver().openInputStream()Uri 标识的内容上获取InputStream

    【讨论】:

    • 我在其他地方使用ByteArrayOutputStream(代码中未显示)。因此,任何可能建议的解决方案都应该考虑到这一点。谢谢!
    • @user1992:然后滚动大量您自己的图像加载代码,包括分叉一个后台线程并使用getContentResolver().openInputStream()Uri 标识的内容上获取InputStream。使用它来填充您的ByteArrayOutputStream,并使用ByteArrayOutputStreamBitmapFactory.decodeByteArray() 中的byte[] 创建您的位图。
    【解决方案2】:

    你可以像下面的代码一样使用

    public void pickImage() {
      Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
      intent.setType("image/*");
      startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
            if (data == null) {
                //Display an error
                return;
            }
            InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
            //Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
        }
    }
    

    【讨论】:

    • 我看不出有什么不同?我错过了什么吗?
    • @user1992:您错过了这样一个事实,即此答案使用getContentResolver().openInputStream()Uri 支持的内容上获得InputStream
    【解决方案3】:

    试试这个你可以直接从onActivityResult设置图片

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
    try{
                final Uri imageUri = data.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
    
                imageView.setImageBitmap(selectedImage);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

    • 谢谢你,看起来没问题。但是,不适用于大图像(大约 4MB)。它不会抛出异常,但要么不显示图像。它显示灰色图像,例如图像太重而无法上传或某事。无论如何要完成这个答案,将Bitmap 转换为Bytearray 这对我有帮助:stackoverflow.com/questions/4989182/…
    • @user1992 永远欢迎
    【解决方案4】:

    //你在片段中所以使用下面的代码希望它的工作 最终的 Uri imageUri = data.getData();

                final InputStream imageStream =getActivity().getContentResolver().openInputStream(imageUri);
    
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
    

    【讨论】:

    • 对不起,但是: 1. 没有比现有答案改进; 2.文字和代码未格式化; 3. 不是fragment 导致了这个特定问题。谢谢
    猜你喜欢
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 2013-11-04
    • 1970-01-01
    相关资源
    最近更新 更多