如何查看 android.hardware.Camera 是否被释放?

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

释放相机对象时出现异常"java.lang.RuntimeException: Method called after release"

以下是我的代码和异常堆栈跟踪。

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

异常堆栈跟踪 -

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[=12=]0(SurfaceView.java:88)
android.view.SurfaceView.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)

似乎对已释放的对象调用了 stopPreview()。

我创建了一个方法,该方法会 return 布尔值,具体取决于相机是否在使用中。下面是一个小片段:

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