'Camera2 API return Camera is error 4 only on SDK 29

I have implemented in my Android application a customization of the camera intent, the basic source code is derived to the officials google github repository.

The only difference is the implementation of the manual management of the flash, this implementation make my life hard, because in certain cases an exception is throwed.

The exception is throwed in the OnError() method that return to me the error code "4" but only on Android OS with SDK >= 29.

I tried to perform different debugging but I was unable to find a solution, can you give me some suggestions?

private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {

    @Override
    public void onOpened(@NonNull CameraDevice cameraDevice) {
        mCameraOpenCloseLock.release();
        mCameraDevice = cameraDevice;
        try {
            SurfaceTexture texture = mTextureView.getSurfaceTexture();
            assert texture != null;
            texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
            Surface surface = new Surface(texture);
            mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            mPreviewRequestBuilder.addTarget(surface);

            mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()), new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                    if(mCameraDevice != null){
                        mCaptureSession = cameraCaptureSession;
                        try {
                            updateAutoFocus();
                            updateFlash(mPreviewRequestBuilder);
                            mPreviewRequest = mPreviewRequestBuilder.build();
                            mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler);
                        } catch (Exception e) {
                            wil.WriteFile("3)PhotoBookAuction - Exception: " + e.toString());
                        }
                    }
                }

                @Override
                public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                    showToast("Create preview configure failed");
                }
            }, mBackgroundHandler);
        } catch (Exception e) {
            wil.WriteFile("3)PhotoBookAuction - Exception: " + e.toString());
        }
    }

    @Override
    public void onDisconnected(@NonNull CameraDevice cameraDevice) {
        mCameraOpenCloseLock.release();
        cameraDevice.close();
        mCameraDevice = null;
    }

    @Override
    public void onError(@NonNull CameraDevice cameraDevice, int error) {
        mCameraOpenCloseLock.release();
        cameraDevice.close();
        mCameraDevice = null;
        Activity activity = getActivity();
        if (null != activity) {
            showToast("Camera is error: " + error);
            activity.finish();
        }
    }
};

UpdateFlash method:

private void updateFlash(CaptureRequest.Builder requestBuilder) {
    if (mFlashSupported) {
        switch (mFlash) {
            case CameraConstants.FLASH_OFF:
                requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
                requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
                break;
            case CameraConstants.FLASH_ON:
                requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
                requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
                break;
            case CameraConstants.FLASH_AUTO:
                requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
                requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
                break;
        }
    }
}


Solution 1:[1]

Hope that your error has been resolved.

The error you're getting is referenced here. https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#constants_1

however, you may try removing the updateAutoFocus() and updateflash() method from the callback just to see if the error is reproduced. If that is the case then adjust your code accordingly.

Edit :
If you're debugging on an emulator, then use a real device for accurate results. Emulators are not reliable for testing camera applications, specially the complex ones, I personally had a rough time with it, your mileage may differ.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1