Not able to launch Gallery from Android Instant app

[亡魂溺海] 提交于 2019-12-30 11:17:27

问题


I created an app which picks up images from images folder in emulator and displays selected image in my layout of application. When I converted my app to instant app, the same code that picks up images from image folder throws exception. I used runtime permissions READ_EXTERNAL_STORAGE.

My code:

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

Exception thrown:

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

Code for runtime permissions:

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

Can anyone help me in solving this issue?


回答1:


Instant Apps are blocked from accessing some features that regular Android apps can access. The exception:

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

Means you have found one such feature. In this case it is Intent.ACTION_GET_CONTENT which is not currently allowed. Support for additional features such as this may be introduced over time.

In addition, Instant Apps do not have access to read or write from external storage so requesting access to READ_EXTERNAL_STORAGE will also not work or throw an exception. See here for a list of permissions that are supported by Instant Apps.



来源:https://stackoverflow.com/questions/44358056/not-able-to-launch-gallery-from-android-instant-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!