尝试一下,一旦你拍照并保存到 SD 卡,并且在 Nougat 中取回 URI 时,这并不是造成问题的意图......
在您的应用程序上实现 FileProvider 非常容易。首先,您需要在 AndroidManifest.xml 的标签下添加 FileProvider 标签,如下所示:AndroidManifest.xml
<?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>
然后在res文件夹下的xml文件夹中创建provider_paths.xml文件。如果文件夹不存在,则可能需要创建。
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>
完毕! FileProvider 现已声明并可供使用。
最后一步是更改 MainActivity.java 中的以下代码行
Uri photoURI = Uri.fromFile(createImageFile());
to
Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider",
createImageFile());
并做了 !您的应用程序现在应该可以在任何 Android 版本(包括 Android Nougat)上完美运行。干杯!