最近做Android项目关于6.0以上拍照权限问题,网上千篇一律浪费了很多时间。
Android6.0以上权限分为:
普通权限(Normal Permissions)
危险权限(Dangerous Permissions)
Android 6.0以上拍照,需要权限:WRITE_EXTERNAL_STORAGE;CAMERA这两个都属于危险权限,需要动态申请:
//权限判断
if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(mActivity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
Fragment.this.requestPermissions(
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
} else {
//跳转到调用系统相机
gotoCarema();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
gotoCarema();
} else {
// Permission Denied
}
}