Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.?

like image 788
Akshat Avatar asked Apr 25 '14 11:04

Akshat


People also ask

What to do if camera is being used by another application?

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 ...

Why does the camera can only be used for one application?

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.

Can I use the camera on my computer for two applications?

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.

Why can’t I use the camera on my computer?

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


Video Answer


2 Answers

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.

like image 54
David Wasser Avatar answered Oct 25 '22 16:10

David Wasser


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().

like image 44
Drew Avatar answered Oct 25 '22 15:10

Drew