javaandroidandroid-studioapk

Launching .apk from Code


Intent install_intent = new Intent(Intent.ACTION_VIEW);
install_intent.setDataAndType(Uri.fromFile(new File(Environment
      .getExternalStorageDirectory() 
          + "app-release.apk")),
          "application/vnd.android.package-archive");
startActivity(install_intent);

So, I am creating an application that checks for updates on launch. it downloads a file from a location, and then launches this intent to run the .apk. Unfortunately, when I do it, it gives me the error "there is a problem parsing the package android studio".

If I navigate to the download path, and run the .apk manually, it works just fine to install.

Any ideas?


Solution

  • Try this one:

    File apkFile = new File(Environment.getExternalStorageDirectory()
                      .getAbsolutePath() + "/app-release.apk");
    Intent install_intent = new Intent(Intent.ACTION_VIEW);
    install_intent.setDataAndType(Uri.fromFile(apkFile), 
         "application/vnd.android.package-archive");
    startActivity(install_intent);