admin 管理员组文章数量: 1103785
前言
今天在做项目功能的时候遇到一个bug exposed beyond app through Intent.getData()
在项目中点击文件路径跳转过去,编译器报错android os FileUriExposedException
导致错误的原因是没有使用FileProvider
在应用间共享文件
对于面向 Android 7.0 的应用,Android 框架执行的 StrictMode API 政策禁止在您的应用外部公开 file:// URI。如果一项包含文件 URI 的 intent 离开您的应用,则应用出现故障,并出现 FileUriExposedException 异常。
解决方法
在AndroidManifest
中配置 FileProvider
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths_public" />
</provider>
其中${applicationId}
一般指向包名
不是androidx的可以参考
<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/file_paths_public" />
</provider>
还需要res
下创建xml文件夹,并在xml文件夹下创建file_paths_public.xml
<!--1、对应内部内存卡根目录:Context.getFileDir()-->
<files-path
name="int_root"
path="/" />
<!--2、对应应用默认缓存根目录:Context.getCacheDir()-->
<cache-path
name="app_cache"
path="/" />
<!--3、对应外部内存卡根目录:Environment.getExternalStorageDirectory()-->
<external-path
name="ext_root"
path="pictures/" />
<!--4、对应外部内存卡根目录下的APP公共目录:Context.getExternalFileDir(String)-->
<external-files-path
name="ext_pub"
path="/" />
<!--5、对应外部内存卡根目录下的APP缓存目录:Context.getExternalCacheDir()-->
<external-cache-path
name="ext_cache"
path="/" />
使用方法
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri contentUri = FileProvider
.getUriForFile(OrderManagerActivity.this, "com.jk.house", file);
intent.setDataAndType(contentUri, "*/*");
总结
2021 继续加油!
本文标签: exposed Android APP getData Intent
版权声明:本文标题:Android exposed beyond app through Intent.getData() 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.520sys.cn/xp/1755028504a1458767.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论