在我的应用程序中,我使用 Camera2 API 在后台进行一些处理。我正在使用众所周知的 Camera2Basic(尽管现在经过大量修改)。一切运行良好,但有一个明显的问题 - 当用户请求相机进行某些操作时(例如启动相机应用程序拍照),我的 Camera2 退出,请参阅 LOGCAT:
E/Camera: Error 2
I/RequestThread-1: Flushing all pending requests.
I/RequestQueue: Repeating capture request cancelled.
I/CameraDeviceState: Legacy camera service transitioning to state ERROR
E/RequestQueue: cancel failed: no repeating request exists for request id: 0
E/CameraDeviceState: Cannot receive result while in state: 0
W/CaptureCollector: previewProduced called with no preview request on queue!
W/MessageQueue: Handler (android.os.Handler) {32bb202} sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (android.os.Handler) {32bb202} sending message to a Handler on a dead thread
... (irrelevant) ...
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)
E/MyApp: disconnected
D/gralloc: gralloc_lock_ycbcr success. format : 11, usage: 3, ycbcr.y: 0x66903000, .cb: 0x66afd401, .cr: 0x66afd400, .ystride: 1920 , .cstride: 1920, .chroma_step: 2
E/BufferItemConsumer: [ImageReader-1920x1080f23m1-19683-0] Failed to release buffer: Unknown error -1 (1)
E/MyApp: closed
现在......在 Camera1 API 中,我能够在报告 E/Camera Error 2 的那一刻准确地截取并采取行动,使用以下方法:
mCamera.setErrorCallback(errorCallback);
但是在 Camera2 API 中,情况很有趣,因为即使我已经配置了我的 StateCallback(请注意 Log.e() 对应于上面的 LOGCAT 输出), onError() 根本不会触发并且 onClosed() 和 onDisconnected( )触发太晚(!):
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice cameraDevice) {
mCameraOpenCloseLock.release();
mCameraDevice = cameraDevice;
createCameraPreviewSession();
}
@Override
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice = null;
Log.e("MyApp", "disconnected");
}
@Override
public void onClosed(@NonNull CameraDevice cameraDevice) {
super.onClosed(cameraDevice);
Log.e("MyApp", "closed");
}
@Override
public void onError(@NonNull CameraDevice cameraDevice, int error) {
Log.e("MyApp", "Here we are "+error);
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice = null;
}
};
如果相机被用户请求接管,为什么 onError() 不会触发?我怎样才能检测到这种情况?我认为能够“利用”以下任何事件都可以解决问题:
I/CameraDeviceState: Legacy camera service transitioning to state ERROR
E/RequestQueue: cancel failed: no repeating request exists for request id: 0
E/CameraDeviceState: Cannot receive result while in state: 0
感谢您的任何反馈。M。