Jleuleu
Jleuleu

Reputation: 1983

Closing the Android CameraDevice is slow and hangs the application

I am writing an Android application that uses the camera.

I took the sample code https://github.com/googlesamples/android-Camera2Basic/ provided by Google. However, I observed that calling the close() method of the CameraDevice takes too long (almost one second on my Samsung Galaxy S8).

This method is called from onPause(), thus the application hangs a little while when the camera fragment is closed.

@Override
public void onPause() {
    closeCamera();
    stopBackgroundThread();
    super.onPause();
}

private void closeCamera() {
    try {
        mCameraOpenCloseLock.acquire();
        if (null != mCaptureSession) {
            mCaptureSession.close();
            mCaptureSession = null;
        }
        if (null != mCameraDevice) {
            mCameraDevice.close(); // This call takes 1 second!
            mCameraDevice = null;
        }
        if (null != mImageReader) {
            mImageReader.close();
            mImageReader = null;
        }
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
    } finally {
        mCameraOpenCloseLock.release();
    }
}

How can I avoid the application to become unresponsive when the closeCamera() method is being called?

I tried to call it from another thread than the UI thread but the application crashes in some cases.

Upvotes: 1

Views: 623

Answers (1)

sunakulto
sunakulto

Reputation: 71

I faced with this problem when my device (mi a1 - oreo) was flashed with patched boot.

If you have device with patched boot too, try to flash it with stock boot. It helped in my case.

Upvotes: 1

Related Questions