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

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);

以上。

相关文章:

  • 2022-12-23
  • 2022-02-16
  • 2022-02-01
  • 2021-11-17
  • 2021-06-12
  • 2021-06-14
  • 2022-01-07
猜你喜欢
  • 2021-11-01
  • 2021-09-28
  • 2022-01-17
  • 2021-09-18
  • 2022-12-23
  • 2021-06-25
相关资源
相似解决方案