我正在尝试启动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
摄像机权限已添加到manifest.xml文件中:
<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" />
这是打开相机的电话:
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);
}
}
}
});
嗨,您可以将清单文件中的这些权限与其他权限一起使用,
<uses-feature
android:name="android.hardware.camera.any"
android:required="true" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
如果仍然无法正常运行,则可能是您使用的是android M,SO,因此您需要以编程方式添加权限。
这是例子
您好,这里是设置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.";
//创建这样的对话框。
// 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;
}
步骤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;
}
步骤3.在您的oncreate方法中调用此方法,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkMultiplePermissions(REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS, MyActivity.this);
} else {
// Open your camera here.
}
步骤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();
}
//创建此接口
public interface OnDialogButtonClickListener {
void onPositiveButtonClicked();
void onNegativeButtonClicked();
}
并在您需要添加权限的活动中实施此操作。
@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() {
}
您可以从此处调用任何权限,您可以在重写方法onRequestPermissionsResult中获得此结果的每个结果。
谢谢你
希望这对您有帮助(Y)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句