首页 > 解决方案 > android通过图库意图选择的相机图像未显示在imageview中

问题描述

//这是我的画廊意图

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



protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
           if (requestCode == 2) {
                Bitmap bm=null;
                if (data != null) {
                    try {
                        bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
                        imageView.setImageBitmap(bm);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                Uri tempUri = getImageUri(getApplicationContext(), bm);
                File finalFile = new File(getRealPathFromURI(tempUri));
            } }

我正在我的 android 应用程序中实现相机和画廊意图。当我通过相机捕获图像时,图像会显示在 imageview 中,但是如果我尝试通过画廊意图显示相同的相机图像,则图像不会显示。

注意:捕获的图像未通过画廊意图显示,但其他文件夹图像显示在 imageview

标签: javaandroid

解决方案


错误 #1:ACTION_PICK不使用 MIME 类型。要么ACTION_GET_CONTENT与 MIME 类型一起使用,要么ACTION_PICK与集合一起使用Uri(例如,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)。

错误 #2:您假设MediaStore知道如何从中获取图像Uri。由于Uri可能不是来自MediaStore,这是一个不正确的假设。使用 Glide 或 Picasso 将图像加载到您的ImageView.

错误 #3:您正在主应用程序线程上加载图像。这将在加载图像时冻结您的 UI。使用 Glide 或 Picasso 将图像加载到您ImageView的 .

错误 #4:您似乎复制了一些“获取Uri位图”的代码。你不需要这样做。你已经有一个Uri图像。它是data.getData(),并且您首先使用它来尝试加载图像。

错误 #5:您似乎复制了一些声称获得“a 的真实路径Uri”的代码。没有可靠的方法可以做到这一点。如果您想要 aFile包含来自图像的数据,请使用ContentResolverandopenInputStream()获取InputStream由 标识的内容Uri,然后使用它将InputStream字节复制到FileOutputStream您控制的某个文件的 a 中。


推荐阅读