Android 处理7.0以上的权限,有些东西会报空,比如打开相机黑屏,FileNotFund,
打开处理资源之前先看看有没有权限
//检测是否有相机和读写文件权限
if (ContextCompat.checkSelfPermission(UIUtils.getContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(UIUtils.getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, CAMERA_PERMISSIONS_REQUEST_CODE);
} else {
//打开相机获取图片
test();
}
Activity回调中
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case CAMERA_PERMISSIONS_REQUEST_CODE: {//调用系统相机申请拍照权限回调
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
test();
} else {
UIUtils.runOnUIToast("你拒绝了相机权限,无法使用二维码扫描");
}
break;
}
}
}
//所有权限列表(常用)
Manifest.permission.CAMERA //相机
Manifest.permission.WRITE_EXTERNAL_STORAGE //存储
Manifest.permission.CALL_PHONE//打电话
部分常用权限(图片来自互联网)
image.png
网友评论