Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if android.hardware.Camera is released?

I got an exception while releasing camera object "java.lang.RuntimeException: Method called after release"

following is my code and exception stack trace.

if (camera != null) {
     camera.stopPreview();               
     camera.release();
     camera = null;
}

Exception stack trace -

java.lang.RuntimeException: Method called after release()
Thread[main,5,main] android.hardware.Camera._stopPreview(Native Method)
android.hardware.Camera.stopPreview(Camera.java:626)
com.s5.selfiemonkey1.activity.Preview.surfaceDestroyed(Preview.java:152)
android.view.SurfaceView.updateWindow(SurfaceView.java:601)
android.view.SurfaceView.access$000(SurfaceView.java:88)
android.view.SurfaceView$3.onPreDraw(SurfaceView.java:183)
android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:680)
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2123)
android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4872)
android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
android.view.Choreographer.doCallbacks(Choreographer.java:579)
android.view.Choreographer.doFrame(Choreographer.java:548)
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
android.os.Handler.handleCallback(Handler.java:800)
android.os.Handler.dispatchMessage(Handler.java:100)
android.os.Looper.loop(Looper.java:194)
android.app.ActivityThread.main(ActivityThread.java:5371)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:525)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)

it look like stopPreview() is called on released object.

like image 923
Amol Patil Avatar asked Sep 27 '22 21:09

Amol Patil


1 Answers

I had made a method which would return me boolean depending of whether camera is in use or not. Below is a small snipet:

    public static Camera checkForCamera(){
    Camera camera = null;
    try {
        camera = Camera.open(); // this line will throw exception if camera is not in use.
    }
    catch (Exception e){
        // if exception is thrown, return your boolean value here...
    }
    return camera; // if instance of camera, if it is not available it will return null.
}
like image 97
Kaveesh Kanwal Avatar answered Oct 11 '22 09:10

Kaveesh Kanwal