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);
}
source
share