1

I have the following in my manifest

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.CAMERA" />

however I receive this error

An error occurred while connecting to camera 0: Status(-8, EX_SERVICE_SPECIFIC): '1: validateClientPermissionsLocked:1165: Caller ... (PID 10153, UID 6049) cannot open camera "0" without camera permission'

I am attempting to get a camera working using this code

public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open();
    } catch (Exception e) {
        Log.e("getCameraInstance", "exception", e);
    }
    return c; // returns null if camera is unavailable
}

How do I get this camera working?

1 Answer 1

8

Need to enable permissions at runtime. The above error is outputted when the 0 indexed camera does not have permissions. Adding permissions to the manifest is not what enables it on the phone... the below code will.

    public static void checkCameraPermissions(Context context){
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED)
        {
            // Permission is not granted
            Log.d("checkCameraPermissions", "No Camera Permissions");
            ActivityCompat.requestPermissions((Activity) context,
                    new String[] { Manifest.permission.CAMERA },
                    100);
        }
    }
2
  • Hey, code only answers aren't the norm around here. Perhaps you could add some verbiage around it, explaining what the OP has done wrong and how this code fixes it? May 16, 2021 at 21:15
  • 1
    @SoftwareEngineer Done! May 18, 2021 at 23:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.