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

5

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);
}
android
android-activity
installation
asked on Stack Overflow Apr 15, 2015 by Jim • edited Feb 5, 2020 by Mike

2 Answers

4

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".

answered on Stack Overflow Apr 15, 2015 by David Wasser
0

I've had this same error when trying to open an URI generated by the DownloadManager, like dm.getUriForDownloadedFile(id);

Fixed it with the following code:

    Uri uri;

    Cursor q = dm.query(new DownloadManager.Query().setFilterById(id));
    if ( q == null ) {
        Toast.makeText(AppContext,
                "FAILED: Unable to read downloaded file" ,
                Toast.LENGTH_LONG).show();
        break;
    }

    q.moveToFirst();
    String path = "file://" + q.getString(q.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
    uri = Uri.parse(path);
    Log.d(TAG,"dm query: " + path );
    intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setDataAndType(uri, dm.getMimeTypeForDownloadedFile(id));
answered on Stack Overflow Mar 26, 2016 by allgood

User contributions licensed under CC BY-SA 3.0