Is there a way to free the camera from various activities after acquiring it from another activity?

I am developing a flashlight application in which there is a regular flashlight in one action and a strobe in one action. Now I purchase a camera in onCreate of Flashlight. But when I do stroboscopic activity, I need to free the camera, purchased when working with FlashLight. I do not want to shoot the camera in the onPause of FlashLight activity, because this will stop the camera, even if the user clicks the home button. I want to release the camera only when the user switches to strobe activity, or he exits the application using the "Back" button. I also want to re-capture the camera if the user returns to flashlight activity from stroboscopic activity. Do they care?

+6
source share
4 answers

Other answers told you not to do this and why. But to answer your question:

Keep a reference to the Camera instance in a static member variable, preferably in a separate class, for example:

 public class Globals { public static Camera myCamera; } 

This variable is available for all your actions as Globals.myCamera .

Place the Camera instance that you get from calling Camera.open() in Globals.myCamera . It will be available for both activities. When you are ready to release the camera, call Globals.myCamera.release() and then set Globals.myCamera to null to indicate that you are no longer in control of the camera.

+7
source

I want to release the camera only when user goes to strobe activity or else he exits the app by back button.

If you do not release the camera resources as soon as possible, the user will not be able to use the camera from any other application. For example, if a user of your application clicks the home button, the camera object will be blocked by your application. This will lead to unpleasant behavior: for example, the user cannot start the Camera application.

As official docs suggest:

It is important . Call release () to release the camera for use by other applications. Applications must immediately release the camera in onPause ()

I want to release the camera only when user goes to strobe activity or else he exits the app by back button

If you do not release the camera resources manually, they will not be released, just press the return button and "exit" from your application.

Also I want to reacquire the camera if the user is coming back to flashlight activity from strobe activity. Is their anyway to do this.?

Just connect to your camera in onResume () and free resources in onPause ().

+1
source

I will not ask you to stop doing what you want, or to tell you about official offers.

You can achieve this functionality using a separate class for working with Camera, and declare all its functions and members static.

  • Whenever you switch to strobe activity, simply release the camera using this static function of a separate class in the onCreate strobe activity.
  • Do the same in the onDestroy() FlashLight activity, But if you finish() FlashLight activity when switching to Strobe activity, then you do not need to perform the above step, and your work will only do this step.
0
source

The best explanation for this is given by David Wasser. We can create a global instance of the camera, and then use it where necessary.

public class Globals {public static camera myCamera; }

Although a workaround is to create buttons for moving to each type of light, and each of them has different classes. In this case, we can leave onpause on the camera as it is, and therefore it does not close the camera at the home button press, but we can release the camera as soon as the user switches to an activity that contains buttons for selecting a camera of light types. Thus, the camera will always be free when choosing.

0
source

All Articles