Android 7.0报错,访问文件时
android.os.FileUriExposedException: file:///storage/emulated/0/auxiliary/1648605378530.txt解决办法
- 声明FIleProvider
- 编写XML文件
- 使用FileProvider
1、在AndroidManifest.xml中添加如下代码
android:authorities :你的 app的包名.fileProvider
android:grantUriPermissions:必须是true,表示授予 URI 临时访问权限
android:exported:必须是false
android:resource:中的filepaths是我们接下来要添加的文件
2、xml 的创建

<?xml version="1.0" encoding="utf-8"?>
<paths>
<!-- 表示 content.getFileDir() 获取到的目录-->
<files-path name="files-path" path="." />
<!-- 表示 content.getCacheDir() 获取到的目录-->
<cache-path name="cache-path" path="." />
<!-- 表示Environment.getExternalStorageDirectory() 指向的目录-->
<external-path name="external_storage_root" path="." />
<!-- 表示 ContextCompat.getExternalFilesDirs() 获取到的目录-->
<external-files-path name="external_file_path" path="." />
<!-- 表示 ContextCompat.getExternalCacheDirs() 获取到的目录-->
<external-cache-path name="external_cache_path" path="." />
<!-- 表示根目录-->
<root-path name="root-path" path="" />
</paths>
3、修改访问路径的代码

版权声明:本文为liubCS原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。