0

We are storing an update of our APK in internal storage of the device. Following is the path of the location:

/storage/emulated/0/Android/data/package_name/files/APK/app_name.apk

We are able to store the updated apk at the above path. But we are unable to install the APK programmatically using java(Because we don’t want to add REQUEST_INSTALL_PACKAGES permission). We decided to atleast open this path so that user can manually install the apk.

SDK level is 32

We have tried multiple ways to open this path, but only Downloads folder is getting opened.

Uri selectedUri = Uri.parse("/storage/emulated/0/Android/data/package_name/files/APK");
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(selectedUri, "*/*");
        intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(intent);

Please suggest a proper way to open the directory.

1 Answers1

0

For similar purpose I'm using below snippet, but be aware thats old app and is still targeting 27, so before e.g. Scoped Storage. Still, afaik, code works well in newer OS versions

public static void sendInstallUpdateIntent(Context ctx, File pendingUpdate) {
    Intent intent;
    if (Build.VERSION.SDK_INT >= 24) {
        Uri uri = FileProvider.getUriForFile(
                ctx, ctx.getApplicationContext().getPackageName() + ".fileprovider", pendingUpdate);
        intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        intent.setData(uri);
    } else {
        Uri uri = Uri.fromFile(pendingUpdate);
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
    }
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    ctx.startActivity(intent);
}

note you have to implement FileProvider, check out official DOC and some basic configuration on SO

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • 1
    Thank you for the suggestion but this doesn’t work for us because we can’t add permission REQUEST_INSTALL_PACKAGES which is must for installing apk in this way. – Tanmay Joshi Jan 13 '23 at 10:21
  • The file location that you specified is owned by a specific app. Only that app and (some) system apps are allowed to access it. There are very few locations (such as Downloads) where apps can save files that are world readable. Modern versions of Android try very hard to make all non-media files owned by an app to be saved in specific places that are automatically deleted when an app is uninstalled. – scottt Jan 13 '23 at 14:21