Android N中的Android android.os.FileUriExposedException错误

时间:2018-07-29 11:01:22

标签: android

我在Orders文件夹中的外部存储中有图像,并且该图像我正试图使用​​Intent共享给外部应用程序

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Orders");
String filename = "OrderID.jpg";
File file = new File(file,filename);
  Intent intent = new Intent(Intent.ACTION_SEND);
  intent.setType("image/*");
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
                try {
                     startActivity(intent);
                } catch (android.content.ActivityNotFoundException ex) {

                }

我已经获得了加载应用程序本身的存储权限,但是每次应用程序崩溃时我都会得到

ndroid.os.FileUriExposedException: file:///storage/emulated/0/Orders/OrderID.jpg exposed beyond app through ClipData.Item.getUri()

任何人都可以帮我解决这个问题吗? 我已经关注了很多Google上的帖子,但是没有用

android.os.FileUriExposedException: file.jpg exposed beyond app through ClipData.Item.getUri()

https://github.com/react-native-community/react-native-share/issues/185

https://github.com/tungdx/android-media-picker/issues/11

https://github.com/MLSDev/RxImagePicker/issues/13

1 个答案:

答案 0 :(得分:1)

要与其他应用共享文件时,必须使用 FileProvider

选中https://developer.android.com/training/secure-file-sharing/setup-sharing

使用 FileProvider.getUriForFile 来获取Uri而不是代码中的Uri.fromFile(file)

FileProvider.getUriForFile(this,"your_package.fileprovider",photoFile);