android: camera onPause/onResume issue

你离开我真会死。 提交于 2019-11-30 00:38:12
stoefln

This is how I fixed it 100%, finally (working on every device I tried it on, including Galaxy S):

I destroyed the camere preview object onResume and reinstantiated all together (like on startup). More details here:

android: I get no stacktrace, phone just hangs

It's a bit late to reach this post but i had the similar problem. First of all, if you using customer rom, it could be the problem of camera driver (mine one X8 runniing 4.0.4). The problem also exists if you press power button to put the phone in standby mode and bring it back in a short time (with or without home screen lock). After try and error i found that put a short delay after shut down camera before super.onPause is the best. My code as below.

  @Override
  public void onPause() {
      // Log.d(TAG,"ccp_onPause");
      closeCamera();
      try {
          Thread.sleep(500);
      } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      super.onPause();
  }

and closeCamera();

   public void closeCamera() {
      if (mCamera != null) {
          mCamera.stopPreview();
          mCamera.setPreviewCallback(null);
          mCamera.lock();
          mCamera.release();
          mCamera=null;
          requestLayout();
        }
    }

My guess is that all you have to do, is to create a setter method in Preview for resetting camera (the local camera object in Preview becomes invalid after onPause() since camera is released, but rest of the state of Preview is still maintained).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!