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

后端 未结 10 1361
一个人的身影
一个人的身影 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:28

    hi you can use these permission in your manifest file with other permission,

    
    
    

    If still it not working then may be you are using android M,SO you need to programmatically add permissions.

    here is example

    hi here is few steps for setup permission for android M and remember you should declare same permission in manifest file as well.

    Step 1. Declare global variable :

     public final int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 1;
    
    //requests for runtime time permissions
    
     String CAMERA_PERMISSION = android.Manifest.permission.CAMERA;

    
     String READ_EXTERNAL_STORAGE_PERMISSION = android.Manifest.permission.READ_EXTERNAL_STORAGE;

    
    String WRITE_EXTERNAL_STORAGE_PERMISSION = android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
    
    
    // for security permissions
    @DialogType
    private int mDialogType;
    private String mRequestPermissions = "We are requesting the camera and Gallery permission as it is absolutely necessary for the app to perform it\'s functionality.\nPlease select \"Grant Permission\" to try again and \"Cancel \" to exit the application.";
    private String mRequsetSettings = "You have rejected the camera and Gallery permission for the application. As it is absolutely necessary for the app to perform you need to enable it in the settings of your device.\nPlease select \"Go to settings\" to go to application settings in your device and \"Cancel \" to exit the application.";
    private String mGrantPermissions = "Grant Permissions";
    private String mCancel = "Cancel";
    private String mGoToSettings = "Go To Settings";
    private String mPermissionRejectWarning = "Cannot Proceed Without Permissions
    We are requesting the location permission as it is necessary for the app to perform search functionality properly.\nPlease select \"Grant Permission\" to try again and \"Cancel \" to deny permission.";
    

    // create dialog like this.

    // type of dialog opened in MainActivity
     @IntDef({DialogType.DIALOG_DENY, DialogType.DIALOG_NEVER_ASK})
     @Retention(RetentionPolicy.SOURCE)
     @interface DialogType {
        int DIALOG_DENY = 0, DIALOG_NEVER_ASK = 1;
     }
    

    Step 2. Use this code in your main activity

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED) {
                    // Call your camera here.
                } else {
                    boolean showRationale1 = shouldShowRequestPermissionRationale(CAMERA_PERMISSION);
                    boolean showRationale2 = shouldShowRequestPermissionRationale(READ_EXTERNAL_STORAGE_PERMISSION);
                    boolean showRationale3 = shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE_PERMISSION);
                    if (showRationale1 && showRationale2 && showRationale3) {
                        //explain to user why we need the permissions
                        mDialogType = ValueConstants.DialogType.DIALOG_DENY;
                        // Show dialog with 
                        openAlertDialog(mRequestPermissions, mGrantPermissions, mCancel, this, MyActivity.this);
                    } else {
                        //explain to user why we need the permissions and ask him to go to settings to enable it
                        mDialogType = ValueConstants.DialogType.DIALOG_NEVER_ASK;
                        openAlertDialog(mRequsetSettings, mGoToSettings, mCancel, this, MyActivity.this);
                    }
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
    
    //check for camera and storage access permissions
    @TargetApi(Build.VERSION_CODES.M)
    private void checkMultiplePermissions(int permissionCode, Context context) {
    
        String[] PERMISSIONS = {CAMERA_PERMISSION, READ_EXTERNAL_STORAGE_PERMISSION, WRITE_EXTERNAL_STORAGE_PERMISSION};
        if (!hasPermissions(context, PERMISSIONS)) {
            ActivityCompat.requestPermissions((Activity) context, PERMISSIONS, permissionCode);
        } else {
            // Open your camera here.
        }
    }
    
    private boolean hasPermissions(Context context, String... permissions) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }
    

    Step 3. Call this method in your oncreate method,

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                checkMultiplePermissions(REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS, MyActivity.this);
       } else {
                // Open your camera here.
       }
    

    Step 4. Dialog for permission deny

    public static void openAlertDialog(String message, String positiveBtnText, String negativeBtnText,
                                final OnDialogButtonClickListener listener,Context mContext) {
    
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.AlertDialogCustom);
        builder.setPositiveButton(positiveBtnText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                listener.onPositiveButtonClicked();
            }
        });
        builder.setPositiveButton(positiveBtnText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                listener.onNegativeButtonClicked();
            }
        });
    
        builder.setTitle(mContext.getResources().getString(R.string.app_name));
        builder.setMessage(message);
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setCancelable(false);
        builder.create().show();
    }
    

    // Create this interface

    public interface OnDialogButtonClickListener {
    
    void onPositiveButtonClicked();
    
    void onNegativeButtonClicked();
    }
    

    and implement this in your activity where need to add permissions.

    @Override
    public void onPositiveButtonClicked() {
        switch (mDialogType) {
            case ValueConstants.DialogType.DIALOG_DENY:
                checkMultiplePermissions(REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS, MyActivity.this);
                break;
            case ValueConstants.DialogType.DIALOG_NEVER_ASK:
                redirectToAppSettings(MyActivity.this);
                break;
    
        }
    }
    
    @Override
    public void onNegativeButtonClicked() {
    
    }
    

    And anyone permission you can call from here and every result you can get in override method onRequestPermissionsResult this one.

    thankyou

    hope this will help you (Y).

提交回复
热议问题