0

I have written a video call application with Activity that has member variable private CameraMediaSource mCameraMediaSource;

Works fine but after a while sometimes camera does not show anymore. Then I kill whole app and it works again. Not sure about how to handle the camera during activity lifecycle methods like onPause(), onResume() etc. Does the cameera at some point need to be released? How do I clear the camera, when it pauses a picture stays on the screen. Thanks

1
  • You should really accept more answers; The one provided works beautifully in my experience.
    – Greyson
    Jun 21, 2012 at 6:44

1 Answer 1

4

Yes, camera should be released. Minimally in onDestroy but probably safer to do it in OnPause.

The lifecycle for camera usage that has worked for me across 1.6 - 3.x is:

  onResume:
   - get a ref to camera with Camera.open();
   - sv = (SurfaceView)this.findViewById(R.id.capture_SurfaceView);
                mHolder = sv.getHolder(); 
                mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
                mHolder.setSizeFromLayout();
                mHolder.addCallback(this); 
  surfaceChanged:
    - Camera.setPreviewDisplayHolder()
    - Camera.startPreview()
  onPause:
    - Camera.stopPreview
    - SurfaceHolder.removeCallback()
    - Camera.release()

This works well for me across the device getting turned off and then back on, or my app otherwise going to background.

Also, be very careful about uncaught exceptions that might leave you not releasing the camera. That can often result in the camera hardware locking up such that killing and restarting your app still wont be able to open camera. If that happens, you'll need to power cycle the device.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.