app代码内部下载更新适配7.0以上系统

最近线上一直报错,错误日志如下

报错系统集中在7.0及10.0,研究发现是因为新系统对文件的访问权限做了修改,

更新用的安装包下载完成后调用系统进行安装时没有权限,需要获取临时访问权限,代码如下

1、manifest文件增加provider

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="你的包名.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths" />
</provider>

2、res目录下新增文件夹xml,创建provider_paths文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <paths>
        <external-path
            name="external"
            path="" />
        <external-files-path
            name="Download"
            path="" />
    </paths>
</resources>

3、新的apk包下载完成后进行适配安装

File file = new File(fileName);
Uri uri = Uri.fromFile(file);
if (!file.exists()) {
   Toast.makeText(context, "下载的安装包不存在", Toast.LENGTH_SHORT).show();
   return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= 24) {
   intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
   Uri contentUri = FileProvider.getUriForFile(context, "你的包名.provider", file);
   intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
   intent.setDataAndType(uri, "application/vnd.android.package-archive");
}
startActivity(intent);

以上。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值