该错误是由于 Android 12 (S) Pixel 模拟器上的相机驱动程序存在问题所致。为了解决此问题,可以通过在代码中添加以下属性来禁用相机2 API:
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
int supportLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
if (supportLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
} else {
cameraIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
}
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
通过'android.intent.extra.USE_FRONT_CAMERA”属性设置为true,应用程序将使用前置摄像头进行拍照。此方法可确保 Android 12 (S) Pixel 模拟器上的相机不会出现 ERROR_CAMERA_DEVICE 错误。