RyanB RyanB - 2 months ago 17
Android Question

Camera2's openCamera() throws SecurityException

Upon using Camera2 class, I hit the following exception when calling

openCamera()
method:

W/System.err: java.lang.SecurityException
W/System.err: at android.os.BinderProxy.transactNative(Native Method)
W/System.err: at android.os.BinderProxy.transact(Binder.java:503)
W/System.err: at android.hardware.ICameraService$Stub$Proxy.connectDevice(ICameraService.java:364)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at android.hardware.camera2.utils.Decorator.invoke(Decorator.java:80)
W/System.err: at java.lang.reflect.Proxy.invoke(Proxy.java:393)
W/System.err: at $Proxy0.connectDevice(Unknown Source)
W/System.err: at android.hardware.camera2.CameraManager.openCameraDeviceUserAsync(CameraManager.java:321)
W/System.err: at android.hardware.camera2.CameraManager.openCamera(CameraManager.java:457)
....


Below are my manifest content & implementation code.

Manifest.xml

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera2" />
<application>
//
</application>


Code to open camera:

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
try {
String camIds[] = cameraManager.getCameraIdList();
if (camIds.length > 0) {
cameraManager.openCamera(camIds[0], new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice cameraDevice) {
Log.d(TAG, "Camera opened");
}

@Override
public void onDisconnected(CameraDevice cameraDevice) {
Log.d(TAG, "Camera disconnected");
}

@Override
public void onError(CameraDevice cameraDevice, int i) {
Log.d(TAG, "Error access camera");
}
}, null);
}
} catch (Exception e) {
e.printStackTrace();
}
}


The code throws exception even after a fresh reboot of the device.

I am on
Samsung Galaxy S6
and
Android 6.0.1
. Anyone points me out what am I missing?

Answer

Your code should be:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION);
} else {
      try {
            String camIds[] = cameraManager.getCameraIdList();
            if (camIds.length > 0) {
                cameraManager.openCamera(camIds[0], new CameraDevice.StateCallback() {
                    @Override
                    public void onOpened(CameraDevice cameraDevice) {
                        Log.d(TAG, "Camera opened");
                    }

                    @Override
                    public void onDisconnected(CameraDevice cameraDevice) {
                        Log.d(TAG, "Camera disconnected");
                    }

                    @Override
                    public void onError(CameraDevice cameraDevice, int i) {
                        Log.d(TAG, "Error access camera");
                    }
                }, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
}