用java下载apk解析包出错_Android 9.0 安装包解析错误 java.lang.SecurityException: Permission Denial解决方案...

在Android 9.0系统中,自动下载更新时遇到安装包解析错误,原因是权限问题。通过日志分析,发现是由于尝试从非导出的FileProvider读取文件导致的SecurityException。为解决此问题,需要在Intent中添加FLAG_GRANT_READ_URI_PERMISSION和FLAG_GRANT_PERSISTABLE_URI_PERMISSION,并调用grantUriPermission()方法。修改后的代码已成功解决了安装包解析失败的问题。
摘要由CSDN通过智能技术生成

在android 9.0自动下载更新时 遇到 安装包解析错误

807638078058

错误示例.png

在android 7.0使用的是同一套框架却没有问题?

然后通过adb命令抓取一下异常发现

12-18 19:21:32.665 4804 5604 W InstallStaging: java.lang.SecurityException: Permission Denial: reading android.support.v4.content.FileProvider uri content://com.***.***.update_app.file_provider/download/update/***.apk from pid=4804, uid=1000 requires the provider be exported, or grantUriPermission()

12-18 19:21:32.665 4804 5604 W InstallStaging: at android.os.Parcel.createException(Parcel.java:1950)

12-18 19:21:32.665 4804 5604 W InstallStaging: at android.os.Parcel.readException(Parcel.java:1918)

12-18 19:21:32.665 4804 5604 W InstallStaging: at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)

12-18 19:21:32.665 4804 5604 W InstallStaging: at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)

12-18 19:21:32.665 4804 5604 W InstallStaging: at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:698)

12-18 19:21:32.665 4804 5604 W InstallStaging: at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1459)

12-18 19:21:32.665 4804 5604 W InstallStaging: at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1296)

12-18 19:21:32.665 4804 5604 W InstallStaging: at android.content.ContentResolver.openInputStream(ContentResolver.java:1016)

12-18 19:21:32.665 4804 5604 W InstallStaging: at com.android.packageinstaller.InstallStaging$StagingAsyncTask.doInBackground(InstallStaging.java:167)

12-18 19:21:32.665 4804 5604 W InstallStaging: at com.android.packageinstaller.InstallStaging$StagingAsyncTask.doInBackground(InstallStaging.java:160)

12-18 19:21:32.665 4804 5604 W InstallStaging: at android.os.AsyncTask$2.call(AsyncTask.java:333)

12-18 19:21:32.665 4804 5604 W InstallStaging: at java.util.concurrent.FutureTask.run(FutureTask.java:266)

12-18 19:21:32.665 4804 5604 W InstallStaging: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)

12-18 19:21:32.665 4804 5604 W InstallStaging: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)

12-18 19:21:32.665 4804 5604 W InstallStaging: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)

12-18 19:21:32.665 4804 5604 W InstallStaging: at java.lang.Thread.run(Thread.java:764)

private Intent installIntent(Context context, String path) {

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.addCategory(Intent.CATEGORY_DEFAULT);

String fileProviderAuthority = getFileProviderAuthority(context);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && null!= fileProviderAuthority) {

Uri fileUri = FileProvider.getUriForFile(context, fileProviderAuthority, new File(path));

intent.setDataAndType(fileUri, "application/vnd.android.package-archive");

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

} else {

intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");

}

return intent;

}

于是我就扒了一下源码对其进行修改

private Intent installIntent(Context context, String path) {

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.addCategory(Intent.CATEGORY_DEFAULT);

String fileProviderAuthority = getFileProviderAuthority(context);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && null!= fileProviderAuthority) {

Uri fileUri = FileProvider.getUriForFile(context, fileProviderAuthority, new File(path));

intent.setDataAndType(fileUri, "application/vnd.android.package-archive");

grantUriPermission(getPackageName(), fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);

} else {

intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");

}

return intent;

}

测试通过,完美安装,没有出现解析失败问题

代码:

android:name="android.support.v4.content.FileProvider"

android:authorities="包名.file_provider"

android:exported="false"

android:grantUriPermissions="true">

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/file_paths"

tools:replace="android:resource" />

//从源码下载的升级下载安装apk的服务

name="storage/emulated/0"

path="." />

/**

* Created by Sun on 2020/12/19.

*/

public class UpdateService extends Service {

public static final String TAG = "UpdateService";

public static final String ACTION = "me.shenfan.UPDATE_APP";

public static final String STATUS = "

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值