Camera2 的 openCamera() 抛出 SecurityException
Camera2's openCamera() throws SecurityException
在使用 Camera2 class 后,我在调用 openCamera()
方法时遇到了以下异常:
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)
....
以下是我的清单内容和实现代码。
Manifest.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
。有人指出我遗漏了什么吗?
您的代码应该是:
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();
}
}
在使用 Camera2 class 后,我在调用 openCamera()
方法时遇到了以下异常:
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)
....
以下是我的清单内容和实现代码。
Manifest.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
。有人指出我遗漏了什么吗?
您的代码应该是:
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();
}
}