三星 SM G9550 android7.0 拍照崩溃。
崩溃日志:
# main(1)
android.os.FileUriExposedException
file:///storage/emulated/0/task/camera/a499e775-75ac-4d39-9712-5469ec1b1b51 exposed beyond app through ClipData.Item.getUri()
解决方案:
1.manifest里面添加配置:
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
${applicationId}需要替换成你的包名
2.在res 下增加xml 包,然后新建file_paths.xml
file_paths内容:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="images" path="task/camera" />
</paths>
3.修改读取uri的代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID +
".fileprovider", file);
}else {
contentUri = Uri.fromFile(file);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
其中BuildConfig.APPLICATION_ID就是包名,和manifest清单文件中的authorities的applicationId 应该相同。即:
BuildConfig.APPLICATION_ID +".fileprovider"==${applicationId}.fileprovider
如果出现
Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
说明刚才说的两个值不相等,或者是xml的路径有问题。
path路径对应:
子节点对应路径例子
files-path→ Context.getFilesDir()
cache-path→ Context.getCacheDir()
external-path→ Environment.getExternalStorageDirectory() → /storage/emulated/0/
external-files-path→ Context.getExternalFilesDir(null)
external-cache-path →Context.getExternalCacheDir()