Android: Permission Denial: starting Intent with revoked permission android.permission.CAMERA

后端 未结 10 1391
一个人的身影
一个人的身影 2020-11-29 07:47

I\'m trying to start a ACTION_IMAGE_CAPTURE activity in order to take a picture in my app and I\'m getting the error in the subject.

Stacktrace:

10条回答
  •  被撕碎了的回忆
    2020-11-29 08:27

    short answer ...its looking for permissions , upon failing permissions it throws exception ; moreover in this case its looking for Two Permissions i.e. first Storage and second Camera.

    long answer.....Give it the permissions write way to work on all Versions of Android.I am looping to get both permissions Storage and Camera, So that it will work with Intent.

    1. maintain in AndroidManifest.xml
    
        
    
        
    
    1. check or request permissions by
      private void myStoragePermission() {
            if (ContextCompat.checkSelfPermission(Activity_Scan_QR.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                myCameraPermission();
            } else {
                //changed here
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
                }
            }
        }
    
        //+10 changed its sinature as Fragment; without it  onRequestPermissionsResult won't bbe called
        private void myCameraPermission() {
            if (ContextCompat.checkSelfPermission(Activity_Scan_QR.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                takePicture();
            } else {
                //changed here
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
                }
            }
        }
    
    
    1. add onRequestPermissionsResult
     @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            switch (requestCode) {
                case REQUEST_WRITE_PERMISSION:
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        myStoragePermission();
                    } else {
    
                        showSnackbar(R.string.act_ScanQR_txt13, R.string.settings,
                                new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        // Build intent that displays the App settings screen.
                                        Intent intent = new Intent();
                                        intent.setAction(
                                                Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                        Uri uri = Uri.fromParts("package",
                                                BuildConfig.APPLICATION_ID, null);
                                        intent.setData(uri);
                                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                    }
                                });
                    }
                case REQUEST_CAMERA_PERMISSION:
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        takePicture();
                    } else {
    
                        showSnackbar(R.string.act_ScanQR_txt14, R.string.settings,
                                new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        // Build intent that displays the App settings screen.
                                        Intent intent = new Intent();
                                        intent.setAction(
                                                Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                        Uri uri = Uri.fromParts("package",
                                                BuildConfig.APPLICATION_ID, null);
                                        intent.setData(uri);
                                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                    }
                                });
    
                    }
            }
        }
    
    

    in above code takePicture(); is where I call for intent (start intent) , upon getting both the Storage and Camera permissions.

    Don't get confused by reading a lot on error ;)

提交回复
热议问题