Android install apk with Intent.VIEW_ACTION not working with File provider

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

My app has an auto-update feature that download an APK and when the download is finished that a Intent.VIEW_ACTION to open the app and let the user install the downloaded apk

         Uri uri = Uri.parse("file://" + destination);          Intent install = new Intent(Intent.ACTION_VIEW);         install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);         install.setDataAndType(uri,             manager.getMimeTypeForDownloadedFile(downloadId));         activity.startActivity(install); 

This works great for all the device

Now with Android 24 apparently we are not allowed any more to start intents with file:/// and after some googling it was advised to use A File Provider

new code:

Intent install = new Intent(Intent.ACTION_VIEW);     install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);     install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);     Uri apkUri = FileProvider.getUriForFile(AutoUpdate.this,         BuildConfig.APPLICATION_ID + ".provider", file);     install.setDataAndType(apkUri,         manager.getMimeTypeForDownloadedFile(downloadId));     activity.startActivity(install); 

Now activity.startActivity(install); throws an error

No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.xxxx.xx.provider/MyFolder/Download/MyApkFile.apk typ=application/vnd.android.package-archive flg=0x4000000 }

Is there any way I can open the APK viewer in Android 7 (24) ?

回答1:

After a lot of trying I have been able to solve this by creating different Intents for anything lower than Nougat as using the FileProvider to create an install intent with Android Versions before Nougat causes the error:

ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.INSTALL_PACKAGE dat=content://XXX.apk flg=0x1 } 

While using a normal Uri on Android Nougat creates the following error:

FileUriExposedException: file:///XXX.apk exposed beyond app through Intent.getData() 

My solution which is working for me with Android N on the emulator and a phone running Android M.

File toInstall = new File(appDirectory, appName + ".apk"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {     Uri apkUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", toInstall);     Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);     intent.setData(apkUri);     intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);     activity.startActivity(intent) } else {     Uri apkUri = Uri.fromFile(toInstall);     Intent intent = new Intent(Intent.ACTION_VIEW);     intent.setDataAndType(apkUri, "application/vnd.android.package-archive");     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     activity.startActivity(intent); } 

UPDATE FOR Android Oreo:

As mentioned in the comments by @Jasongiss

"It's worth mentioning that as of Oreo you must also add the REQUEST_INSTALL_PACKAGES permission to your manifest. Otherwise it just silently fails."



回答2:

This could be the problem, you have

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);  

in your example it should be

install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

as install is the name of the intent.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!