Occur Camera.release() ошибка Предварительный просмотр камеры использовать TextureView на Android

Я использую 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 ответ

Используйте предварительный просмотр камеры и отпустите ее внутри обработчика. Так что камера не остановится сразу после того, как ваша активность будет уничтожена. Это остановится в фоновом режиме.

Другие вопросы по тегам