android auth_error ai_generated true

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

Also available as: JSON · Markdown · 中文
95%Fix Rate
90%Confidence
1Evidence
2023-03-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Android 12 (API 31) active
Android 13 (API 33) active
Android 14 (API 34) active
CameraX 1.3.0 active

Root Cause

App attempts to access the camera without the required android.permission.CAMERA being granted at runtime, or the permission is missing from AndroidManifest.xml.

generic

中文

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

Official Documentation

https://developer.android.com/training/permissions/requesting

Workarounds

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

中文步骤

  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);

Dead Ends

Common approaches that don't work:

  1. 90% fail

    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.

  2. 70% fail

    Using ContextCompat.checkSelfPermission() incorrectly (e.g., checking against a wrong package name) may return GRANTED even when permission is denied.

  3. 80% fail

    Requesting the permission in a background thread without UI callback leads to the request being ignored by the system.