android S 上 安装apk出现android.os.FileUriExposedException

简介: android S 上 安装apk出现android.os.FileUriExposedException

android S 上 安装apk出现android.os.FileUriExposedException


报错:

android.os.FileUriExposedException: file:///data/user/0/com.example.overlay.   exposed beyond app through Intent.getData()

原因:

andorid7.0系统以后,引入“私有目录被限制访问”,“StrictMode API 政策”导致的问题。解决办法很简单。就是用新的方式获取uri。

” StrictMode API 政策” 是指禁止向你的应用外公开 file:// URI。 如果一项包含文件 file:// URI类型 的 Intent 离开你的应用,应用失败,并出现 FileUriExposedException 异常。

解决方法

1.在AndroidManifest.xml中添加如下代码

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="com.example.overlay.idea.fileprovider"
    android:grantUriPermissions="true"
    android:exported="false">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

注意事项:

authorities:app的包名.fileProvider

grantUriPermissions:必须是true,表示授予 URI 临时访问权限

exported:必须是false 为true则会报安全异常。

resource:中的@xml/file_paths是我们接下来要添加的文件

2、在res目录下新建一个xml文件夹,并且新建一个file_paths的xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <root-path path="/data/user/0/com.example.overlay.idea/cache/" name="files_root" />
    <root-path path="." name="external_storage_root" />
</paths>

path:需要临时授权访问的路径(.代表所有路径)

name:访问路径名字

3.修改代码适配Android N

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Log.e(TAG, "installAPk: " + apkFile.getAbsolutePath());
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(this, "com.example.overlay.idea.fileprovider",apkFile);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

4.申请权限,否则无法跳转至apk安装界面

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

(308条消息) android安装apk出现android.os.FileUriExposedException_android.os.fileuriexposedexception: file:///storag_冬瓜去哪儿的博客-CSDN博客

Failed to find configured root that contains的解决办法不知道这里能帮到你吗 - 简书 (jianshu.io)

目录
相关文章
|
1月前
|
关系型数据库 MySQL 数据库
【Mac os系统】安装MySQL数据库
本文详细介绍了在Mac OS系统上安装MySQL数据库的步骤,包括下载、安装、配置环境变量、启动服务、授权设置以及解决常见问题,并提供了一些常用的MySQL命令。
79 0
【Mac os系统】安装MySQL数据库
|
2月前
|
Linux 虚拟化 数据安全/隐私保护
部署05-VMwareWorkstation中安装CentOS7 Linux操作系统, VMware部署CentOS系统第一步,下载Linux系统,/不要忘, CentOS -7-x86_64-DVD
部署05-VMwareWorkstation中安装CentOS7 Linux操作系统, VMware部署CentOS系统第一步,下载Linux系统,/不要忘, CentOS -7-x86_64-DVD
|
25天前
|
Windows
Windows操作系统部署安装Kerberos客户端
详细介绍了在Windows操作系统上部署安装Kerberos客户端的完整过程,包括下载安装包、安装步骤、自定义安装路径、修改环境变量、配置hosts文件和Kerberos配置文件,以及安装后的验证步骤。
40 3
Windows操作系统部署安装Kerberos客户端
|
25天前
|
Ubuntu 网络安全 开发工具
Ubuntu19.04的安装过程详解以及操作系统初始化配置
本文详细介绍了Ubuntu 19.04操作系统的安装过程、初始化配置、网络设置、软件源配置、SSH远程登录以及终端显示设置。
56 1
Ubuntu19.04的安装过程详解以及操作系统初始化配置
|
1月前
|
编解码 Linux 虚拟化
超详细VMware虚拟机安装Win10操作系统过程图解
这篇文章提供了一个详细的VMware虚拟机安装Windows 10操作系统的图解教程,包括了从创建虚拟机到安装操作系统的全过程,以及安装后的一些基本设置,如屏幕分辨率调整等。作者还提到了后续会分享关于磁盘分区的创建过程。
超详细VMware虚拟机安装Win10操作系统过程图解
|
30天前
|
Java Android开发 Windows
使用keytool查看Android APK签名
本文介绍了如何使用Windows命令行工具和keytool查看APK的签名信息,并提供了使用AOSP环境中的signapk.jar工具对APK进行系统签名的方法。
65 0
使用keytool查看Android APK签名
|
1月前
|
Ubuntu 安全 iOS开发
Kylin操作系统安装及使用指南
Kylin操作系统安装及使用指南
|
1月前
|
Android开发
将AAB(Android App Bundle)转换为APK
将AAB(Android App Bundle)转换为APK
67 1
|
1月前
|
Android开发 开发者
Android、Flutter为不同的CPU架构包打包APK(v7a、v8a、x86)
Android、Flutter为不同的CPU架构包打包APK(v7a、v8a、x86)
67 1
|
1月前
|
Android开发
解决android apk安装后出现2个相同的应用图标
解决android apk安装后出现2个相同的应用图标
165 2

热门文章

最新文章