Skip to content Skip to sidebar Skip to footer

Android: How To Open An Apk File After Downloading For Auto-update?

I have an application that I'd like to add auto-update functionality (it's not in the marketplace). I have all the code in place that would check to see if there is an update avai

Solution 1:

The answer above was for pre-API-24.

If your app targets API 24 or more (and it should), you need to use something else (otherwise you get FileUriExposedException, as described here) :

FileapkFile=newFile(...);
    Intentintent=newIntent(Intent.ACTION_VIEW);
    UrifileUri= android.support.v4.content.FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", apkFile);
    intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(intent);

provider_paths.xml:

<?xml version="1.0" encoding="utf-8"?><pathsxmlns:android="http://schemas.android.com/apk/res/android"><!--<external-path name="external_files" path="."/>--><external-pathpath="Android/data/YOUR_PACKAGE_NAME"name="files_root" /><external-pathpath="."name="external_storage_root" /></paths>

where YOUR_PACKAGE_NAME is your app's package name.

manifest:

<providerandroid:name="android.support.v4.content.FileProvider"android:authorities="${applicationId}.provider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/provider_paths"/></provider>

Solution 2:

This will start the installation process

FileapkFile=newFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/packageName.apk");
Intentintent=newIntent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
startActivity(intent);

Post a Comment for "Android: How To Open An Apk File After Downloading For Auto-update?"