openCamera()
在使用 Camera2 类时,我在调用方法时遇到了以下异常:
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)
....
以下是我的清单内容和实现代码。
清单.xml
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera2" />
<application>
//
</application>
打开相机的代码:
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();
}
}
即使在设备重新启动后,代码也会引发异常。
我在Samsung Galaxy S6
和Android 6.0.1
。有人指出我错过了什么吗?