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

萝らか妹 提交于 2019-12-23 10:56:06

问题


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.


回答1:


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.
}


来源:https://stackoverflow.com/questions/32305215/how-to-check-if-android-hardware-camera-is-released

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!