Android 7.0 安装应用报错:android.os.FileUriExposedException

[BLOG时间:2017.11.02]

测试 App下载更新包升级的时候,测试的手机已经被我升级到7.0,下载完毕更新包时手机报错
[android.os.FileUriExposedException]

错误信息如下:

android.os.FileUriExposedException:file:///storage/emulated/0/Download/appName-2.3.0.apk
exposed beyond app through Intent.getData()

查了一下资料 大概是:
Google 近几个版本开始收紧Android权限
6.0开始收紧了app权限申请
7.0开始Android不再允许在app中把file://Uri暴露给其他app,包括但不局限于通过Intent或ClipData 等方法。

你可能会用到的权限:

 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

解决办法[一]

在manifest中加入:

//用replace是因为小弟在项目中用到了外部包me.iwf.photopicker,它自身有manifest
//不加replace 编译的时候会报错,如果有引入额外包 就这样写吧 ,没有引入也不影响。一劳永逸

<application> 
      <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="[你的包名].fileProvider"
            android:exported="false"
            android:grantUriPermissions="true" 
            tools:replace="android:authorities"> 
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"
                tools:replace="android:resource"/>
        </provider>
        ...
<application>

解析:

authorities:app的包名.fileProvider
grantUriPermissions:必须是true,表示授予 URI 临时访问权限
exported:必须是false
resource:中的@xml/file_paths是我们接下来要添加的文件

然后在res目录下新建一个xml文件夹,并且新建一个file_paths的xml文件(如下图)
路径:src->main->res->xml
路径:src->main->res->xml

file_path.xml:

<?xml version="1.0" encoding="utf-8"?>
<!--7.0下载更新 安装更新需要权限 
<paths>
    <external-path
        name="files_root"
        path="Android/data/[你的包名](和上面相同)/"/>
    <external-path
        name="external_storage_root"
        path="."/>
</paths>

解析:

path:需要临时授权访问的路径(.代表所有路径)
name:就是你给这个访问路径起个名字

7.0与7.0以下 安装应用的代码判断

    public static void installApk(Context context, File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        //判断是否是AndroidN以及更高的版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            //网上很多都是让写BuildConfig.APPLICATION_ID + ".fileProvider"应用的包名 不过我觉得容易发生错误,所以这样可以直接解决问题 
            Uri contentUri = FileProvider.getUriForFile(context, "[你的包名](和上面相同).fileProvider", file);

            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

解决办法就是这样 除了报名,其他一路复制就可以了

解决办法[二]

这个简单一点,只需要新建一个[你的AppContext] 然后继承系统application

 <application
        xmlns:tools="http://schemas.android.com/tools"
        android:name="[你的AppContext]"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.NoActionBar">
        ....
 </application>

[你的AppContext] 类的onCreate()中添加如下代码:

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

图片示例:

对于代码如上

感谢:
1.解决 Android N 7.0 上 报错:android.os.FileUriExposedException
http://blog.csdn.net/yy1300326388/article/details/52787853


有什么疑问可以留言
如果感觉帮到了你 提升了效率 也可以打赏一下小弟

支付宝
这里写图片描述

微信
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值