问题
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.
回答1:
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.
回答2:
One solution maybe setting the surfaceview to invisible and visible again in onResume(), this makes the surfaceview destroy and recreates.
来源:https://stackoverflow.com/questions/7186007/camera-example-bug-when-locking-unlocking-device