android camera release error from PreviewCallback-ThrowExceptions

Exception or error:

So I’ve read over the Camera API and couldn’t find anything on this. I’m using the Camera to grab frames and it works perfectly, until I try to release the camera. I replicated the error in one sequence of calls:

camera = Camera.open();
camera.setPreviewDisplay(getHolder());
Parameters params = camera.getParameters();
List<Size> ls = params.getSupportedPreviewSizes();
Size size = ls.get(1);
this.width = size.width;
this.height = size.height;
params.setPreviewSize(size.width, size.height);
camera.setParameters(params);
camera.setDisplayOrientation(90);
camera.setPreviewCallback(this);
camera.startPreview();
camera.stopPreview();
camera.release();

The error I get is

03-22 13:31:42.592: E/AndroidRuntime(14152): java.lang.RuntimeException: Method called after release()
03-22 13:31:42.592: E/AndroidRuntime(14152):    at android.hardware.Camera.setHasPreviewCallback(Native Method)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at android.hardware.Camera.access$600(Camera.java:114)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:545)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at android.os.Looper.loop(Looper.java:130)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at android.app.ActivityThread.main(ActivityThread.java:3684)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at java.lang.reflect.Method.invokeNative(Native Method)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at java.lang.reflect.Method.invoke(Method.java:507)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:845)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at dalvik.system.NativeStart.main(Native Method)

So whatever is going on is not in one of my functions.
If I comment out the camera.setPreviewCallback(this); then this error does not appear, but I obviously lose my callback, which is the whole point of including the camera in my app.

How to solve:

You have to unset preview callback before camera.release(), after camera.stopPreview():

camera.setPreviewCallback(null);

Otherwise it might get called after camera has been released.

###

As a side note, a complete releaseCamera method looks like:

// release Camera for other applications
private void releaseCamera() {
    // check if Camera instance exists
    if (mCamera != null) {
        sPreviewing = false;
        // first stop preview
        mCamera.stopPreview();
        // then cancel its preview callback
        mCamera.setPreviewCallback(null);
        // and finally release it
        mCamera.release();
        // sanitize you Camera object holder
        mCamera = null;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *