分析原因:
android7.0以后file:// 不被允许作为一个附加的 Uri 的意图,否则会抛出 FileUriExposedException 。
解决方案:
1、创建一个provider_paths.xml 文件在 res文件夹下的 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>
2、在 AndroidManifest.xml中application申明
<provider android:authorities=" “包名或者applicationId”.provider" android:name="android.support.v4.content.FileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider>3、使用
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.M){ originalUri = Uri.fromFile(camerFile); }else{ /** * 7.0 调用系统相机拍照不再允许使用Uri方式,应该替换为FileProvider * 并且这样可以解决MIUI系统上拍照返回size为0的情况 */ originalUri = FileProvider.getUriForFile(PerInformationActivity.this,"到这里就大功告成了。包名或者applicationId" + ".provider",camerFile);}