Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

android - How to recover camera preview from sleep?

I have an application that shows camera preview and I would like the user to be able to put the phone to sleep and then wake it so that my application will recover correctly. The problem is that when returning from sleep the camera preview won't restart.

I have implemented the camera preview as is presented in the api demos, but it seems that the api demo example works only through sheer luck. In the example the screen orientation is forced to landscape, which means that the phone will go through configuration change every time the phone goes to sleep, since the lockscreen is in portrait mode. If the portrait mode is used in the camera preview application (like in mine), the bug surfaces.

I have gathered that the bug is related to recreation of the surfaceview. The surface should be destroyed always when going to onPause and then recreated after onResume, but this doesn't happen when going to sleep. It seems that I have to destroy the whole activity and then recreate it to get the camera preview to work again. I would like to be able to just recreate the surfaceview.

Is there a way to force the recreation of the surfaceview other than just recreating the whole activity?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...