发生Camera.release()错误相机预览在android上使用TextureView

时间:2017-07-25 03:59:49

标签: android camera

我使用textureview,预览相机屏幕。

在我的应用中,拨打另一个应用。

名为onSurfaceTextureDestoryed

@Override
public boolean onSurfaceTextureDestryed(SurfaceTexture surfaceTexture) {
  mCamera.stopPreview();
  mCamera.release();
}

如果是其他应用,请拨打我的应用。

发生RuntimeException Camera is being used after Camera.release() was called

FATAL EXCEPTION: main
      java.lang.RuntimeException: Camera is being used after Camera.release() was called
          at android.hardware.Camera.setPreviewTexture(Native Method)
          at kr.co.iosystem.blackeyeonandroid.video.CameraTextureView.onSurfaceTextureAvailable(CameraTextureView.java:83)
          at android.view.TextureView.getHardwareLayer(TextureView.java:368)
          at android.view.View.updateDisplayListIfDirty(View.java:15157)
          at android.view.View.draw(View.java:15954)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
          at android.view.View.draw(View.java:16187)
          at android.view.View.updateDisplayListIfDirty(View.java:15180)
          at android.view.View.draw(View.java:15954)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
          at android.view.View.updateDisplayListIfDirty(View.java:15175)
          at android.view.View.draw(View.java:15954)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
          at android.view.View.draw(View.java:16187)
          at android.view.View.updateDisplayListIfDirty(View.java:15180)
          at android.view.View.draw(View.java:15954)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
          at android.view.View.updateDisplayListIfDirty(View.java:15175)
          at android.view.View.draw(View.java:15954)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
          at android.view.View.updateDisplayListIfDirty(View.java:15175)
          at android.view.View.draw(View.java:15954)
          at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
          at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
          at android.view.View.draw(View.java:16187)
          at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2690)
          at android.view.View.updateDisplayListIfDirty(View.java:15180)
          at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:282)
          at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:288)
          at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:323)
          at android.view.ViewRootImpl.draw(ViewRootImpl.java:2609)
          at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2428)
          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2061)
          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6007)
          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
          at android.view.Choreographer.doCallbacks(Choreographer.java:670)
          at android.view.Choreographer.doFrame(Choreographer.java:606)
          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
          at android.os.Handler.handleCallback(Handler.java:739)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:148)
          at android.app.ActivityThread.main(ActivityThread.java:5415)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

如果删除mCamera.release(),则不会发生RuntimeException。 但我应该添加mCamera.release()

如何处理此错误?

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
  try {
      mCamera.setPreviewTexture(surfaceTexture);  //error
      mCamera.startPreview();
   } catch (IOException e) {
      mCamera.release();
      mCamera = null;
      e.printStackTrace();
   }
}

1 个答案:

答案 0 :(得分:0)

在处理程序内使用相机停止预览和释放。因此,在您的活动被破坏后,相机不会立即停止。它将在后台停止。