一 :声明常量
private static final int PERMISSIONS_REQUEST_CAMERA = 800;
二:注册权限
cameraView.getCameraControl().setPermissionCallback(permissionCallback);
三:请求权限
private PermissionCallback permissionCallback = new PermissionCallback() {
@Override
public boolean onRequestPermission() {
ActivityCompat.requestPermissions(BaiduOCRActivity.this,new String[] {Manifest.permission.CAMERA},PERMISSIONS_REQUEST_CAMERA);
return false;
}
};
四:权限执行
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSIONS_REQUEST_CAMERA: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
cameraView.getCameraControl().refreshPermission();
} else {
Toast.makeText(getApplicationContext(), com.baidu.ocr.ui.R.string.camera_permission_required, Toast.LENGTH_LONG).show();
}
break;
}
default: break;
}
}