user7500403
user7500403

Reputation:

Installing APK file programatically in android

I am making an android app which allows me to download and install an .apk file.

Problem

I am able to download the file but not able to install it.

code to install .apk is as follows

Uri fileURI = FileProvider.getUriForFile(MainActivity.this,
                            BuildConfig.APPLICATION_ID + ".provider",
                            apkFile);

Intent installAPK = new Intent();

installAPK.setAction(Intent.ACTION_VIEW);

installAPK.setDataAndType(fileURI, apkFile,
                         "application/vnd.android.package-archive");

startActivity(installAPK);

Question

What am I doing wrong here? How can i install the downloaded .apk file?

Upvotes: 1

Views: 403

Answers (1)

Vicky
Vicky

Reputation: 5136

To Install apk programmatically

Uri fileURI = FileProvider.getUriForFile(MainActivity.this,
                            BuildConfig.APPLICATION_ID + ".provider",
                            apkFile);
Intent installAPK = new Intent(Intent.ACTION_INSTALL_PACKAGE);
installAPK.setData(fileURI);
installAPK.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(installAPK);

Upvotes: 1

Related Questions