-1

This is my code:

i.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
i.setDataAndType(Uri.parse(FileUtil.getPublicDir(Environment.DIRECTORY_DOWNLOADS).concat("/Vertretungsplan.apk")), "application/vnd.android.package-archive");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

However, when I start the activity, the Package Installer won't show as "open with".

BSMP
  • 4,596
  • 8
  • 33
  • 44

2 Answers2

1

That is not a valid Uri. A Uri has a scheme; the value that you are trying to parse does not.

On Android 6.0 and below, use Uri.fromFile() to build the Uri for your file.

On Android 7.0+, since the file scheme is banned, you will need to configure FileProvider for your app and use a Uri from it. Unfortunately, that won't work for Android 6.0 and below, as the package installer could not handle content Uri values back then. So, you need to check the version of Android that your app is running on and use the appropriate approach.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • That's not the problem, the Uri is valid. –  Apr 25 '19 at 19:46
  • @RichardS.: No, it is not. It has no scheme. You can tell that by logging its value to Logcat, or by inspecting it in a debugger. Also note that your `Intent` seems to lack an action. I am guessing that you are using `ACTION_VIEW`. If so, you will need to fix your `Uri` to have a scheme (`file` for Android 6.0 and below, `content` for Android 7.0 and above), as I wrote in the answer. – CommonsWare Apr 25 '19 at 19:54
  • I left the first lines out to minimize the code. The Uri has the right scheme and the intent also has the `ACTION_VIEW` action. –  Apr 25 '19 at 22:28
  • @RichardS.: "The Uri has the right scheme" -- then perhaps you should edit your question and post the actual code. The code in your question results in a `Uri` without a scheme. – CommonsWare Apr 25 '19 at 22:33
0

Add REQUEST_INSTALL_PACKAGES permission to your manifest

ronginat
  • 1,910
  • 1
  • 12
  • 23