本文介绍了如何检查android.hardware.Camera是否已发布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
释放相机对象"java.lang.RuntimeException:释放后调用的方法"时出现异常
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;
}
异常堆栈跟踪-
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().
it look like stopPreview() is called on released object.
推荐答案
我已经制定了一种方法,根据是否使用相机,该方法将使我返回布尔值.下面是一个小片段:
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.
}
这篇关于如何检查android.hardware.Camera是否已发布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!