Error detecting error while trying to install apk from local file

I made an application in which I download the APK from the internal server, save it locally and want to suggest the user to install it.
My code is as follows:

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 an APK file, so there’s no problem downloading, but when I try to start 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:
It seems that if I change the code as follows, it will work! It seems like there is a problem with 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);
}
+4
source share
2 answers

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

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

path ( : file://), .

Android Activity, URI, , , URI "".

+4

URI, DownloadManager, dm.getUriForDownloadedFile(id);

:

    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));
0

All Articles