简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM