Android拍照时候出现的这个异常,查了半天是因为没有在AndroidManifest定义FileProvider导致的。
我是使用知乎Matisse里面的拍照时候直接闪退的。
导致异常使用的地方如下:包名后面追加.fileprovider(com.healthmanagement.fileprovider)
Matisse.from(this).choose(MimeType.ofImage()).countable(false).maxSelectable(9)
.restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
.thumbnailScale(0.85f).imageEngine(GlideEngine()).capture(true) //显示相机可见
.captureStrategy(CaptureStrategy(true, "com.healthmanagement.fileprovider"))
.showPreview(true) // Default is true
.forResult(100)
异常显示如下:
2020-03-17 10:29:35.776 14683-14683/com.healthmanagement E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.healthmanagement, PID: 14683
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Pictures/JPEG_20200317_102935.jpg
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
解决办法:
1、AndroidManifest的application块中添加代码:(注意<meta-data>标签中provider_paths名字),后面要用到。
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
android:multiprocess="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
2、在res文件夹下面创建一个xml文件夹,同时在xml中创建一个provider_paths(和第一步里面名字要一直)的xml,如图:

3、打开新创建的provider_paths文件:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<root-path
name="root"
path="" />
</paths>
网友评论