我是Android 6.0编码的新手请为以下代码提供解决方案:
当我提供READ_EXTERNAL_STORAGE
和WRITE_EXTERNAL_STORAGE
之类的运行时权限时,它会显示像
java.lang.SecurityException:权限拒绝:启动Intent {act = android.media.action.IMAGE_CAPTURE flg = 0x3 cmp = com.motorola.camera / .Camera clip = {text / uri-list U:文件:///storage/emulated/0/Pictures/MyAppNew%20File%20Upload/IMG_20160401_110234.jpg}(有附加内容)}来自ProcessRecord {ed96564 26955:com.social.nocializer / u0a259}(pid = 26955,uid = 10259 )具有撤销权限android.permission.CAMERA
MediaStore.ACTION_IMAGE_CAPTURE
和MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE
运行时权限无效...
注意:READ_EXTERNAL_STORAGE
适用于打开图库时
答案 0 :(得分:20)
您必须为此管理运行时权限,因为您在AndroidManifest中定义的任何权限都不会自动授予。因此,与以下方法一样,您可以检查您的许可是否获得批准
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_REQUEST_CODE);
}
此处,MY_REQUEST_CODE是您可以定义的静态常量,它将再次用于requestPermission对话框回调。现在,您将需要回调对话框结果:
@Override
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_REQUEST__CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Now user should be able to use camera
}
else {
// Your app will not have this permission. Turn off all functions
// that require this permission or it will force close like your
// original question
}
}
}
答案 1 :(得分:8)
@Ronak Solution为我工作但有一些以下更改,因为我们只需要检查那些高于Android M的设备。
if( ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.CAMERA},
5);
}
}
使用crl + o复制覆盖以下方法,粘贴可能会导致错误:D
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 5) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Now user should be able to use camera
}
else {
// Your app will not have this permission. Turn off all functions
// that require this permission or it will force close like your
// original question
}
}