Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No activity error found when trying to install an apk from a local file

I made an application where I download an APK from an internal server, save it locally and want to prompt the user to install it.
My code is the following:

protected void onPostExecute(String path) {
    Intent promptInstall = new Intent(Intent.ACTION_VIEW);
    promptInstall.setDataAndType(Uri.parse(path), "application/vnd.android.package-archive");
    promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(promptInstall);
}

The path has the file of the APK so no problem with the download but when trying to launch the activity I get:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=/storage/sdcard/download/internalLocalApplication.apk typ=application/vnd.android.package-archive flg=0x10000000 }
     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1765)
     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1602)
     at android.app.Activity.startActivityFromFragment(Activity.java:4340)
     at android.app.Activity.startActivityFromFragment(Activity.java:4312)
     at android.app.Fragment.startActivity(Fragment.java:1075)
     at android.app.Fragment.startActivity(Fragment.java:1054)

What am I doing wrong here?

UPDATE:
Seems that if I change the code as follows it works! Seems that there is an issue with the Uri?

@Override
protected void onPostExecute(String path) {
    Uri fileLoc = Uri.fromFile(new File(path));
    Intent promptInstall = new Intent(Intent.ACTION_VIEW);
    promptInstall.setDataAndType(fileLoc, "application/vnd.android.package-archive");
    promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(promptInstall);
}
like image 321
Jim Avatar asked Sep 28 '22 07:09

Jim


1 Answers

When you call Uri.parse(path), the path argument needs to contain something like this:

file:///storage/sdcard/download/internalLocalApplication.apk

Currently your path argument doesn't contain the scheme (ie: the file:// part), which is why it can't be properly parsed.

When Android then looks for an Activity that can view this URI, It can't find one that matches, since the URI has no "scheme".

like image 117
David Wasser Avatar answered Oct 03 '22 08:10

David Wasser