Is there any way to release a camera from different activity after acquiring it from a different activity.?

StackOverflow https://stackoverflow.com/questions/23291190

Question

I am developing a flashlight App in which there is a normal flashlight in one activity and strobe light in one activity. Now I am acquiring the camera in onCreate of Flashlight activity. But when I am oving to strobe activity, i need to release the camera acquired by FlashLight activity. I dont want to release the camera in onPause of FlashLight activity because that would stop the camera even if user presses the home button. I want to release the camera only when user goes to strobe activity or else he exits the app by back button. 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.?

Was it helpful?

Solution

Other answers have 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, like this:

public class Globals {
    public static Camera myCamera;
}

This variable is available to all of your activities as Globals.myCamera.

Put the instance of Camera that you get from calling Camera.open() into Globals.myCamera. This will be available to 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 no longer have control of the camera.

OTHER TIPS

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 be unable to use camera from any other application. For example, if user of your app would press a home button, camera object would be left locked by your app. That would result in troublesome behaviour: for example, user failing to start a Camera application.

As official docs suggest:

Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately 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 camera resources manually, they will not be released simply be pressing back button and "exiting" your app.

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 release resources in onPause().

I won't ask you to stop doing what you want or tell you about the official suggestions.

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

  • Whenever you switch to strobe activity just release the camera using that separate class's static function in onCreate of strobe activity.
  • Do the same thing in onDestroy() of FlashLight activity, But if you finish() FlashLight activity when you switch to Strobe activity then you don't need to perform above step and your work will be done with this step only.

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

public class Globals { public static Camera myCamera; }

Although another workaround is to create buttons to move into each type of light and each and everyone having different classes. In such case we can leave the onpause on the camera as it is, and so it does not close the camera on home button press, but we can release the camera as soon as the user goes to the activity which contains buttons to select the camera light types. So the camera will be free always while selecting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top