# SecurityException: 相机权限被拒绝。用户拒绝了相机权限，或清单中未声明该权限。

- **ID:** `android/securityexception-camera-permission-denied`
- **领域:** android
- **类别:** auth_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

应用尝试访问相机，但未在运行时授予所需的 android.permission.CAMERA 权限，或者 AndroidManifest.xml 中缺少该权限声明。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Android 12 (API 31) | active | — | — |
| Android 13 (API 33) | active | — | — |
| Android 14 (API 34) | active | — | — |
| CameraX 1.3.0 | active | — | — |

## 解决方案

1. ```
   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); }
   ```
2. ```
   Use the AndroidX Activity Result API for a cleaner permission request: registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> { if (isGranted) { openCamera(); } }).launch(Manifest.permission.CAMERA);
   ```
3. ```
   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);
   ```

## 无效尝试

- **** — 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. (90% 失败率)
- **** — Using ContextCompat.checkSelfPermission() incorrectly (e.g., checking against a wrong package name) may return GRANTED even when permission is denied. (70% 失败率)
- **** — Requesting the permission in a background thread without UI callback leads to the request being ignored by the system. (80% 失败率)
