赞
踩
升级到android9.0,在Android 8.1.0 oppo A5 报以下错误:
- 2021-04-07 09:46:38.078 18428-20051/? W/PackageInstaller.PackageInstaller: Error staging apk from content URI
- java.io.FileNotFoundException: No content provider: content://com.xxx.xxx.fileprovider/camera_photos/xxx123.apk
- at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1415)
- at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1266)
- at android.content.ContentResolver.openInputStream(ContentResolver.java:986)
- at com.android.packageinstaller.ck.a(OppoPackageInstallerActivity.java:3278)
- at com.android.packageinstaller.ck.doInBackground(OppoPackageInstallerActivity.java:3214)
- at android.os.AsyncTask$2.call(AsyncTask.java:333)
- at java.util.concurrent.FutureTask.run(FutureTask.java:266)
- at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
- at java.lang.Thread.run(Thread.java:764)
在排查了以下代码:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
- public void installApplication(File fileapk) {
- this.mApkFile=fileapk;
- //int targetSdkVersion = this.getApplicationInfo().targetSdkVersion;
- //8.0后需要判断权限,targetSdkVersion小于26无需开启权限
- if (Build.VERSION.SDK_INT >=AndroidBuild.VERSION_CODES.O) {
- boolean hasInstallPermission = isHasInstallPermissionWithO(this);
- if (!hasInstallPermission) {
- startInstallPermissionSettingActivity();
- return;
- }
- }
- installApp();
- }
- private void installApp() {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- //intent.setDataAndType(FileProviderUtils.getFileForUri(this,mApkFile,intent), "application/vnd.android.package-archive");
- FileProviderUtils.setIntentDataAndType(NewBaseApplication.getAppContext(),intent, "application/vnd.android.package-archive", mApkFile, true);
- startActivity(intent);
- ActivityStackUtils.getInstance().finishAllActivity(this, true);//部分手机需要注释掉这行代码
- android.os.Process.killProcess(android.os.Process.myPid());
- }
- @RequiresApi(api = AndroidBuild.VERSION_CODES.O)
- private boolean isHasInstallPermissionWithO(Context context){
- if (context == null){
- return false;
- }
- return context.getPackageManager().canRequestPackageInstalls();
- }
FileProviderUtils.java
- public static Uri getFileForUri(Context context, File mHeaderFile) {
- if (Build.VERSION.SDK_INT >= AndroidBuild.VERSION_CODES.N) {
- Uri imageUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", mHeaderFile);//通过
- return imageUri;
- } else {
- return Uri.fromFile(mHeaderFile);
- }
- }
-
- public static void setIntentDataAndType(Context context,Intent intent,String type,File file,boolean writeAble) {
- if (Build.VERSION.SDK_INT >= AndroidBuild.VERSION_CODES.N) {
- Uri uri=getFileForUri(context, file);
- intent.setDataAndType(uri, type);
- intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
- if (writeAble) {
- intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
- }
- grantPermissions(context,intent,uri,writeAble);
- } else {
- intent.setDataAndType(Uri.fromFile(file), type);
- }
- }
-
- public static void grantPermissions(Context context, Intent intent, Uri uri, boolean writeAble) {
- int flag = Intent.FLAG_GRANT_READ_URI_PERMISSION;
- if (writeAble) {
- flag |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
- }
- intent.addFlags(flag);
- List<ResolveInfo> resInfoList = context.getPackageManager()
- .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
- for (ResolveInfo resolveInfo : resInfoList) {
- String packageName = resolveInfo.activityInfo.packageName;
- context.grantUriPermission(packageName, uri, flag);
- }
- }
最后把 手机管家-->权限隐私-->关联启动管理 里的当前应用 权限打开,就不会报错了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。