Java android通过gallery intent选择的相机图像未显示在imageview中

Java android通过gallery intent选择的相机图像未显示在imageview中,java,android,Java,Android,//这是我的画廊意图 Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent,2); protected void onActivityResult(int requestCode, int r

//这是我的画廊意图

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中,但如果我尝试通过图库意图显示相同的相机图像,则图像不会显示

注意:捕获的图像不是通过gallery intent显示的,而是在imageview中显示的其他文件夹图像

Bug#1:
操作_PICK
不使用MIME类型。对MIME类型使用
ACTION\u GET\u CONTENT
,或对集合
Uri
使用
ACTION\u PICK
(例如,
MediaStore.Images.Media.EXTERNAL\u CONTENT\u Uri

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

Bug#3:您正在主应用程序线程上加载映像。这将在加载图像时冻结您的UI。使用Glide或Picasso将图像加载到您的
ImageView
,因为他们知道如何在背景线程上执行该操作

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


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

尝试使用以下代码,希望对您有所帮助

Uri selectedImage = data.getData();
Bitmap bitmap = null;
try {
   bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(selectedImage));
    } catch (FileNotFoundException e) {
         e.printStackTrace();
   }

 imageView.setImageBitmap(bitmap);

这方面的例子很多。你可以检查它是否正确并在你的代码中实现我试过一些例子,然后只有我发布了这个question@Vishali您还需要在AndroidManifest.xml文件中设置FileProvider。你做到了吗?来吧