当前位置:   article > 正文

Android 8.1.0 oppo A5 解析包时出现问题 解决思路_error staging apk from content uri java.io.filenot

error staging apk from content uri java.io.filenotfoundexception: open faile

升级到android9.0,在Android 8.1.0   oppo A5 报以下错误:

  1. 2021-04-07 09:46:38.078 18428-20051/? W/PackageInstaller.PackageInstaller: Error staging apk from content URI
  2. java.io.FileNotFoundException: No content provider: content://com.xxx.xxx.fileprovider/camera_photos/xxx123.apk
  3. at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1415)
  4. at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1266)
  5. at android.content.ContentResolver.openInputStream(ContentResolver.java:986)
  6. at com.android.packageinstaller.ck.a(OppoPackageInstallerActivity.java:3278)
  7. at com.android.packageinstaller.ck.doInBackground(OppoPackageInstallerActivity.java:3214)
  8. at android.os.AsyncTask$2.call(AsyncTask.java:333)
  9. at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  10. at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
  11. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
  12. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
  13. at java.lang.Thread.run(Thread.java:764)

在排查了以下代码:

    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
  1. public void installApplication(File fileapk) {
  2. this.mApkFile=fileapk;
  3. //int targetSdkVersion = this.getApplicationInfo().targetSdkVersion;
  4. //8.0后需要判断权限,targetSdkVersion小于26无需开启权限
  5. if (Build.VERSION.SDK_INT >=AndroidBuild.VERSION_CODES.O) {
  6. boolean hasInstallPermission = isHasInstallPermissionWithO(this);
  7. if (!hasInstallPermission) {
  8. startInstallPermissionSettingActivity();
  9. return;
  10. }
  11. }
  12. installApp();
  13. }
  14. private void installApp() {
  15. Intent intent = new Intent(Intent.ACTION_VIEW);
  16. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  17. //intent.setDataAndType(FileProviderUtils.getFileForUri(this,mApkFile,intent), "application/vnd.android.package-archive");
  18. FileProviderUtils.setIntentDataAndType(NewBaseApplication.getAppContext(),intent, "application/vnd.android.package-archive", mApkFile, true);
  19. startActivity(intent);
  20. ActivityStackUtils.getInstance().finishAllActivity(this, true);//部分手机需要注释掉这行代码
  21. android.os.Process.killProcess(android.os.Process.myPid());
  22. }
  23. @RequiresApi(api = AndroidBuild.VERSION_CODES.O)
  24. private boolean isHasInstallPermissionWithO(Context context){
  25. if (context == null){
  26. return false;
  27. }
  28. return context.getPackageManager().canRequestPackageInstalls();
  29. }
FileProviderUtils.java
  1. public static Uri getFileForUri(Context context, File mHeaderFile) {
  2. if (Build.VERSION.SDK_INT >= AndroidBuild.VERSION_CODES.N) {
  3. Uri imageUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", mHeaderFile);//通过
  4. return imageUri;
  5. } else {
  6. return Uri.fromFile(mHeaderFile);
  7. }
  8. }
  9. public static void setIntentDataAndType(Context context,Intent intent,String type,File file,boolean writeAble) {
  10. if (Build.VERSION.SDK_INT >= AndroidBuild.VERSION_CODES.N) {
  11. Uri uri=getFileForUri(context, file);
  12. intent.setDataAndType(uri, type);
  13. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  14. if (writeAble) {
  15. intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  16. }
  17. grantPermissions(context,intent,uri,writeAble);
  18. } else {
  19. intent.setDataAndType(Uri.fromFile(file), type);
  20. }
  21. }
  22. public static void grantPermissions(Context context, Intent intent, Uri uri, boolean writeAble) {
  23. int flag = Intent.FLAG_GRANT_READ_URI_PERMISSION;
  24. if (writeAble) {
  25. flag |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
  26. }
  27. intent.addFlags(flag);
  28. List<ResolveInfo> resInfoList = context.getPackageManager()
  29. .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
  30. for (ResolveInfo resolveInfo : resInfoList) {
  31. String packageName = resolveInfo.activityInfo.packageName;
  32. context.grantUriPermission(packageName, uri, flag);
  33. }
  34. }

 

最后把 手机管家-->权限隐私-->关联启动管理  里的当前应用 权限打开,就不会报错了

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/188976
推荐阅读
相关标签
  

闽ICP备14008679号