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

报错系统集中在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);
以上。
针对线上系统报错问题,本文详细介绍了如何通过修改Android应用的manifest文件和资源配置,使用FileProvider来解决新系统中文件访问权限受限导致的安装失败问题。
1404

被折叠的 条评论
为什么被折叠?



