我在释放相机对象时遇到异常“java.lang.RuntimeException:释放后调用的方法”
以下是我的代码和异常堆栈跟踪。
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$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)
看起来在释放的对象上调用了stopPreview()。
答案 0 :(得分:4)
我已经制作了一个方法,可以根据相机是否正在使用而返回布尔值。下面是一个小小的狙击手:
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.
}