Cosimo Sguanci Cosimo Sguanci - 1 month ago 8
Android Question

Android intent doesn't work on .apk files

I'm developing a File Manager, and I open files this way:

fileName = fileToOpen.getName();
fileName = fileName.substring(fileName.lastIndexOf('.') + 1);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".provider", fileToOpen);
intent.setDataAndType(uri, MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileName));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);


This can handle almost all extension, but when I click on .apk files an ActivityNotFoundException gets thrown. I would like to launch the android installation screen.

Answer

Only on Android 7.0+ will the package installer know how to work with content Uri values. For earlier versions of Android, you have no choice but to have the APK on external storage somewhere and use a file Uri.

Comments