开发人员应使用以下步骤来获取Android设备上的相机权限。
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION); }
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_CAMERA_PERMISSION) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission granted } else { // Permission denied Toast.makeText(getActivity(), "Camera permission denied", Toast.LENGTH_SHORT).show(); } } }
注意:在API级别23(Android Marshmallow)及更高版本中,您需要在运行时请求危险权限,而不是在安装时请求。
上一篇:Android上的无限闪屏屏幕