Search code examples
androidapkandroid-fileprovider

Installing APK from SD card (problem with FileProvider)


I have a problem with a file provider. I have successfully downloaded an apk to my phone. The apk is either stored on SD or internal phone storage.

(SD Card)
/storage/3565-6665/Android/data/com.mytest/files/My App Name/Download/app-v1.3.apk

(internal storage)
/storage/emulated/0/Android/data/com.mytest/files/My App Name/Download/app-v1.3.apk

The problem is that from internal storage the file is found and can be installed but on the SD card it does not work and throws folling exception:

Failed to find configured root that contains /storage/3565-6665/Android/data/com.mytest/files/My App Name/Download/app-v1.3.apk

My path.xml looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." /></paths>

And the FileProvider in the manifest:

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/path" />
</provider>

And I'm installing the apk in the following way:

File file = my_file;
Uri fileUri = Uri.fromFile(file); //for Build.VERSION.SDK_INT <= 24
if (Build.VERSION.SDK_INT >= 24) {
    fileUri = FileProvider.getUriForFile(getApplication(), BuildConfig.APPLICATION_ID + ".provider", file);
}
Intent promptInstall = new Intent(Intent.ACTION_VIEW, fileUri);
promptInstall.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
promptInstall.setDataAndType(fileUri, "application/vnd.android.package-archive");
promptInstall.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
promptInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(promptInstall);

Solution

  • FileProvider does not support removable storage. Either:

    • Always download to what you call "internal storage" (but from the Android SDK standpoint is external storage)

    • Have the user choose the APK on removable storage using ACTION_OPEN_DOCUMENT, and use the Uri you get from that for your ACTION_VIEW Intent

    • Create your own ContentProvider that can serve files from your app's removable storage directories, then use that ContentProvider instead of FileProvider for your Uri

    • Switch to using PackageInstaller, as this bypasses the need for any sort of ContentProvider, and it addresses the fact that using ACTION_VIEW for app installation is deprecated on Android 10+