darkzangel
darkzangel

Reputation: 949

Camera example bug when locking/unlocking device

I'm working with the camera and I'm using the exact same example given in the documentation : http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

I running the example on a Xoom with Android 3.1 and 3.2.

My problem is when I lock and unlock the Xoom, the camera doesn't come back. The image stay the same as the last one before I locked the tablet and the red light doesn't come back either.

If anyone could help me, I will appreciate.

Upvotes: 4

Views: 2872

Answers (2)

Jason Kuang
Jason Kuang

Reputation: 261

One solution maybe setting the surfaceview to invisible and visible again in onResume(), this makes the surfaceview destroy and recreates.

Upvotes: 0

mmeyer
mmeyer

Reputation: 3608

By lock and unlock, do you mean when the screen sleeps or the device power switch is pressed putting the device to sleep and then woken back up?

If so, I suspect you need to release the camera resources in your onPause and then start the preview again in onResume, via the surface view callback.

In the Android 2.2 and 2.3 apps I have that deal with camera the pattern I use is:

onCreate:
 - get reference to the camera
onResume:
- 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.

Upvotes: 8

Related Questions