Camera example bug when locking/unlocking device

不问归期 提交于 2020-01-03 15:58:27

问题


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

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