SecurityException: 相机权限被拒绝。用户拒绝了相机权限,或清单中未声明该权限。
SecurityException: Camera permission denied. The user has denied the camera permission, or the permission is not declared in the manifest.
ID: android/securityexception-camera-permission-denied
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Android 12 (API 31) | active | — | — | — |
| Android 13 (API 33) | active | — | — | — |
| Android 14 (API 34) | active | — | — | — |
| CameraX 1.3.0 | active | — | — | — |
根因分析
应用尝试访问相机,但未在运行时授予所需的 android.permission.CAMERA 权限,或者 AndroidManifest.xml 中缺少该权限声明。
English
App attempts to access the camera without the required android.permission.CAMERA being granted at runtime, or the permission is missing from AndroidManifest.xml.
官方文档
https://developer.android.com/training/permissions/requesting解决方案
-
Declare <uses-permission android:name="android.permission.CAMERA" /> in AndroidManifest.xml. Then in code, request the permission: if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_CODE); } -
Use the AndroidX Activity Result API for a cleaner permission request: registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> { if (isGranted) { openCamera(); } }).launch(Manifest.permission.CAMERA); -
If the user has permanently denied the permission, show a rationale dialog and guide them to Settings: Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", getPackageName(), null)); startActivity(intent);
无效尝试
常见但无效的做法:
-
90% 失败
Adding the permission to AndroidManifest.xml without requesting it at runtime on Android 6+ still causes the error because the user hasn't granted it.
-
70% 失败
Using ContextCompat.checkSelfPermission() incorrectly (e.g., checking against a wrong package name) may return GRANTED even when permission is denied.
-
80% 失败
Requesting the permission in a background thread without UI callback leads to the request being ignored by the system.