1、在AndroidManifest.xml中添加如下代码
...
...
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
补充:若项目中引用了图片选择库,可能也会引用到此provider,就需要在meta-data标签中添加
tools:replace="android:resource",然后就可以了。
2、在res目录下新建一个xml文件夹,并且新建一个provider_paths的xml文件
3、修改代码
Uri photoURI = Uri.fromFile(createImageFile());
变成:
Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());