异常信息:android.os.FileUriExposedException: file:///storage/emulated/0/…
说明:通过URI实现本地图片文件查看的时候,在android 7.0以上运行报错,7.0以下运行正常
解决步骤:
1、在AndroidManifest中添加如下内容:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<application
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
</manifest>
2、在res目录下新建一个xml文件夹,并且新建一个provider_paths的xml文件
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
3、使用
将下面代码
Uri photoURI = Uri.fromFile(createImageFile());
修改成:
Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());
解决问题结束。
参考自:
https://www.cnblogs.com/netcorner/p/6542373.html