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.?
What to Do If Camera Is Being Used by another Application? 1 Quit all programs that are using the camera.. 2 Disable your Windows Firewall.. 3 Troubleshoot the camera.. 4 Update apps on your computer.. 5 Change camera access setting.. The camera on your computer can not be used for two or more applications at the same time. More ...
The camera can only be used for one application once. When you see this error, you’d better quit all programs that are using the camera and then only open the application you need to use. The camera is blocked by Windows Firewall. There is something wrong with your camera.
The camera on your computer can not be used for two or more applications at the same time. If can use the camera because it is being used by another app, you need to close all programs that are using the camera.
The camera on your computer can not be used for two or more applications at the same time. If can use the camera because it is being used by another app, you need to close all programs that are using the camera. Then, you can only open your needed application to use the camera. Fix 2: Disable Your Windows Firewall
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.
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With