How to ask permission to access gallery on android M.?

后端 未结 2 1770
无人共我
无人共我 2020-11-30 12:12

I have this app that will pick image to gallery and display it to the test using Imageview. My problem is it won\'t work on Android M. I can pick image but won\'t show on my

2条回答
  •  长情又很酷
    2020-11-30 13:09

    public void pickFile() {
            int permissionCheck = ContextCompat.checkSelfPermission(getActivity(),
                    CAMERA);
            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                        getActivity(),
                        new String[]{CAMERA},
                        PERMISSION_CODE
                );
                return;
            }
            openCamera();
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
                                               @NonNull int[] grantResults) {
            if (requestCode == PERMISSION_CODE) {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    openCamera();
                }
            }
        }
    
        private void openCamera() {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAMERA_CODE);
        }
    
    
    
    

提交回复
热议问题