Android:Permission Denial:使用已撤销权限android.permission.CAMERA启动Intent

Dav*_*aiz 29 android android-securityexception

我正在尝试启动ACTION_IMAGE_CAPTURE活动,以便在我的应用中拍照并且我在主题中收到错误.

堆栈跟踪:

FATAL EXCEPTION: main
Process: il.ac.shenkar.david.todolistex2, PID: 3293
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=com.google.android.GoogleCamera/com.android.camera.CaptureActivity } from ProcessRecord{22b0eb2 3293:il.ac.shenkar.david.todolistex2/u0a126} (pid=3293, uid=10126) 
with revoked permission android.permission.CAMERA
Run Code Online (Sandbox Code Playgroud)

摄像机权限被添加到manifest.xml fie:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
Run Code Online (Sandbox Code Playgroud)

这是打开相机的电话:

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.statusgroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
            RadioButton rb = (RadioButton) findViewById(R.id.donestatusRBtn);
            if(rb.isChecked())
            {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                }
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

Gia*_*men 47

删除此权限

  <uses-permission android:name="android.permission.CAMERA"/>
Run Code Online (Sandbox Code Playgroud)

我在Android 7中执行我的应用程序时遇到此错误.测试后我注意到用户权限不在项目A中但它在项目B中,我只在android 5设备中测试过.所以我删除了项目B中的权限,以便在其他针对android 7的设备上运行它,它最终可以打开.

在adittion我添加了Android建议的文件提供者代码https://developer.android.com/training/camera/photobasics.html 希望这会有所帮助.

  • 奇怪怎么去掉权限,居然去掉了需要权限的错误!!!! (5认同)
  • 我的天啊!认真地它正在发挥作用。尽管从开发的开始阶段我就在菜单中添加了这一行。 (3认同)
  • 但我的应用程序既像通过 Intent 调用相机,又像内置相机。给予许可使工作内置且未经许可意图相机正在工作 (3认同)
  • 再加上我的一个答案,您的答案是唯一对我有用的答案。虽然这很奇怪! (2认同)

Sav*_*een 18

嗨,您可以在清单文件中使用这些权限并获得其他权限,

<uses-feature
    android:name="android.hardware.camera.any"
    android:required="true" />
<uses-feature
    android:name="android.hardware.camera.autofocus"
    android:required="false" />
Run Code Online (Sandbox Code Playgroud)

如果仍然无法正常工作,那么您可能正在使用Android M,那么您需要以编程方式添加权限.

这是一个例子

嗨这里有几个步骤来设置Android M的权限,并记住你也应该在清单文件中声明相同的权限.

步骤1.声明全局变量:

 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</string>
<string name="explanation_permission_location_request">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.";
Run Code Online (Sandbox Code Playgroud)

//创建这样的对话框.

// 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;
 }
Run Code Online (Sandbox Code Playgroud)

第2步.在主要活动中使用此代码

@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;
}
Run Code Online (Sandbox Code Playgroud)

步骤3.在oncreate方法中调用此方法,

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

步骤4.权限拒绝的对话框

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();
}
Run Code Online (Sandbox Code Playgroud)

//创建此界面

public interface OnDialogButtonClickListener {

void onPositiveButtonClicked();

void onNegativeButtonClicked();
}
Run Code Online (Sandbox Code Playgroud)

并在需要添加权限的活动中实现此功能.

@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() {

}
Run Code Online (Sandbox Code Playgroud)

你可以从这里调用任何权限,你可以在覆盖方法onRequestPermissionsResult中获得每个结果.

谢谢

希望这会对你有帮助(Y).

  • 你在跟我开玩笑吗,我只是做了同样的例子,它的工作正常在这里是链接https://www.dropbox.com/s/w29sljy0zpwwm61/MyApplication.zip?dl=0 (2认同)

Mon*_*SSI 8

在我的情况下,问题与我的模拟器权限有关,

要解决此问题:

1-转到模拟器的"设置".

2-查找应用和通知.

3-单击"添加权限".

见图:https://i.stack.imgur.com/z4GfK.png

4-选择列表的摄像头.

5-在提供的列表中查找您的应用程序.

6-启用相机.

见图:https://i.stack.imgur.com/dJ8wG.pngEnjoy

现在你可以在你的模拟器上使用你的相机了:)


Fav*_*rem 8

这是我解决我的方法:

首先,我认为当您尝试在没有完全权限的情况下在(SDK < 26)上使用设备相机时会出现问题。

是的,即使您已经包含此权限:

<uses-permission android:name="android.permission.CAMERA"/>
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我改变的是这个

<uses-permission android:name="android.permission.CAMERA" 
                 android:required="true" 
                 android:requiredFeature="true"/>
Run Code Online (Sandbox Code Playgroud)

来自 Android Docs 的这些信息可能真的很有帮助

如果您的应用程序使用但不需要摄像头来运行,则设置android:requiredfalse. 这样,Google Play 将允许没有摄像头的设备下载您的应用程序。然后你有责任在运行时通过调用来检查相机的可用性hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)。如果相机不可用,则应禁用相机功能。


sau*_*dav 5

private String [] permissions = {"android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.ACCESS_FINE_LOCATION", "android.permission.READ_PHONE_STATE", "android.permission.SYSTEM_ALERT_WINDOW","android.permission.CAMERA"};
Run Code Online (Sandbox Code Playgroud)

在您OnCreate添加以下内容:

int requestCode = 200;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    requestPermissions(permissions, requestCode);
}
Run Code Online (Sandbox Code Playgroud)