Search code examples
javaandroid-cameraandroid-permissions

cannot open camera "0" without camera permission


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?


Solution

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