Android 處理7.0以上的權(quán)限,有些東西會報空,比如打開相機黑屏,FileNotFund,
打開處理資源之前先看看有沒有權(quán)限
//檢測是否有相機和讀寫文件權(quán)限
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回調(diào)中
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case CAMERA_PERMISSIONS_REQUEST_CODE: {//調(diào)用系統(tǒng)相機申請拍照權(quán)限回調(diào)
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
test();
} else {
UIUtils.runOnUIToast("你拒絕了相機權(quán)限,無法使用二維碼掃描");
}
break;
}
}
}
//所有權(quán)限列表(常用)
Manifest.permission.CAMERA //相機
Manifest.permission.WRITE_EXTERNAL_STORAGE //存儲
Manifest.permission.CALL_PHONE//打電話
部分常用權(quán)限(圖片來自互聯(lián)網(wǎng))