调用相册打开图片 startActivity时报了个错
startActivityForResult error:file:///storage/emulated/0/Android/data/xxx.jpg exposed beyond app through Intent.getData()
调查发现是AndroidStudio7.0之后官方对这权限做了调整:官方解答:出于用户隐私安全考虑,对于面向 Android 7.0 的应用,Android 框架执行的 StrictMode API 政策禁止在您的应用外部公开 file://URI。传递软件包网域外的 file://URI 可能给接收器留下无法访问的路径。因此,尝试传递 file://URI会触发 FileUriExposedException。分享私有文件内容的推荐方法是使用 FileProvider。
1.添加在Manifest.xml中
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="xxx.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
2. 在新建的资源文件@xml/provider_paths中添加如下代码:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path path="." name="external_storage_root" />
</paths>
下面为文件的子节点
3.URI获取方法
出现该问题时:
File file = new File(filePath);
Uri uri = Uri.fromFile(file);
正确的方法:
Uri contentUri = FileProvider.getUriForFile(context, "xxx.fileprovider", file);