How to restore camera preview from sleep?

I have an application that shows a camera preview, and I would like the user to be able to remove the phone and then wake it up so that my application recovers correctly. The problem is that when you return from sleep, the camera preview does not restart.

I implemented the camera preview presented in the api demos, but it seems that the demo api example only works thanks to pure luck. In this example, the screen orientation is forced to change to landscape, which means that each time you switch to standby mode, the phone enters the configuration change mode, since the lock screen is in portrait mode. If portrait mode is used in a camera preview application (for example, in a mine), surface errors.

I realized that the error is related to the recreation of the surface. The surface should always be destroyed when switching to onPause, and then recreated after onResume, but this does not happen when you go to sleep. It seems that I should destroy all the activity and then recreate it in order to start the camera preview again. I would like to be able to simply recreate the surface overview.

Is there a way to make it possible to recreate a superficial vision, and not just recreate all the activity?

+7
source share
2 answers

One solution, perhaps, again draws attention to the invisibility and visibility of the surface in onResume (), which allows you to remove and recreate the surface.

+5
source

I had the same problem and this is why:

The onPause method does not recycle the CameraPreview class that I made (which implements SurfaceView callbacks). Instead of trying to recreate an instance of this entire object, I just updated the link to the camera object that I passed in order to be either

null 

or

 cameraPreview.setCamera(mCamera); 

I called the getCameraInstance method to initialize the camera, and then passed it to the preivew object.

Now the problem is here:

 private void initializeCameraView(){ RelativeLayout preview = (RelativeLayout)rootView.findViewById(R.id.camera_preview); preview.addView(cameraPreview); } 

in onResume, I called this method to initialize the cameraPreview object. However, it hung because I was trying to add another kind of camera to the preview view . It was my fix, simple and simple. If it already exists, delete it and then return it. Hope this helps!

 private void initializeCameraView(){ RelativeLayout preview = (RelativeLayout)rootView.findViewById(R.id.camera_preview); //removes the problem with the camera freezing onResume if(cameraPreview != null) { preview.removeView(cameraPreview); } preview.addView(cameraPreview); } 
+3
source

All Articles