使用Java在Android中打开存储目录

omvjsjqw  于 2023-01-15  发布在  Android
关注(0)|答案(1)|浏览(184)

我们正在设备的内部存储中存储APK的更新。以下是位置的路径:
/存储/模拟/0/Android/数据/软件包名称/文件/APK/应用名称.apk
我们可以在上述路径中存储更新的APK。但我们无法使用Java以编程方式安装APK(因为我们不想添加REQUEST_INSTALL_PACKAGES权限)。我们决定至少打开此路径,以便用户可以手动安装APK。
SDK级别为32
我们尝试了多种方法来打开此路径,但只有下载文件夹被打开。

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

请建议打开目录的正确方法。

4c8rllxm

4c8rllxm1#

出于类似的目的,我使用下面的片段,但要知道,这是旧的应用程序,仍然是针对27,所以之前,例如,范围存储。仍然,afaik,代码在较新的操作系统版本中工作良好

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

注意,您必须实现FileProvider,请查看正式的DOC和一些basic configuration on SO

相关问题