无法从 Android 免安装应用启动图库

Not able to launch Gallery from Android Instant app

我创建了一个应用程序,它从模拟器中的图像文件夹中选取图像,并在我的应用程序布局中显示选定的图像。当我将我的应用程序转换为即时应用程序时,从图像文件夹中拾取图像的相同代码会引发异常。我使用了运行时权限 READ_EXTERNAL_STORAGE.

我的代码:

private static final int PICK_IMAGE_REQUEST = 234;
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);

抛出异常:

java.lang.SecurityException: Not allowed to start activity Intent { 
          act=android.intent.action.GET_CONTENT typ=*/* }

运行时权限代码:

private static final int REQUEST_WRITE_STORAGE = 112;
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public boolean checkPermission()
    {
        int currentAPIVersion = Build.VERSION.SDK_INT;
        if(currentAPIVersion>=android.os.Build.VERSION_CODES.M)
        {
            if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)) {

                   Toast.makeText(MainActivity.this,"Permission Denied...",Toast.LENGTH_LONG).show();
                } else {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_WRITE_STORAGE);
                    return  true;
                }
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

谁能帮我解决这个问题?

免安装应用无法访问常规 Android 应用可以访问的某些功能。异常:

java.lang.SecurityException: Not allowed to start activity Intent...

表示您找到了一个这样的功能。在这种情况下,它是 Intent.ACTION_GET_CONTENT 当前不允许的。随着时间的推移,可能会引入对此类其他功能的支持。

此外,免安装应用无法从外部存储读取或写入,因此请求访问 READ_EXTERNAL_STORAGE will also not work or throw an exception. See here 以获取免安装应用支持的权限列表。