tags:

views:

45

answers:

2

As part of my widget, I use an instance of the Camera object.

This is what I want to do. The user will click on my widget, I get an instance of the Camera(if it's not already stored), use it, then store it. If they click the widget again, I want to use that same instance that I used previously.

Is this possible?

EDITED: I can't release the Camera(android.hardware.Camera) until the user clicks on the widget the second time. So the user clicks on the widget the first time, I get the camera and hold on to it until they click the widget again.

The problem I am running into is on the second click, I am trying to get the Camera again, which I can't because I currently have it in use.

A: 

No, it is not possible for an app widget to "store" arbitrary objects.

If the "Camera" object is android.hardware.Camera, you should be using it and releasing it as soon as you are done, anyway, so other applications can use the camera.

CommonsWare
I can't release the camera until the user clicks on the widget the second time. Is there any other way of handling this?
Eclipsed4utoo
@Eclipsed4utoo: I am interpreting that you really are meaning `android.hardware.Camera`. In that case, what you want is impossible several times over -- not only can you not hold onto a `Camera` (as noted above), but you need a `SurfaceView` for the camera preview, which is not possible with an app widget. You are welcome to use the `Camera` with an `Activity`.
CommonsWare
I am talking about that camera, but I am not using the SurfaceView(and don't need it).
Eclipsed4utoo
@Eclipsed4utoo: The `Camera` will not work without a `SurfaceView` and an active preview, AFAIK, so the user knows the camera is in operation.
CommonsWare
I am doing this for the flash, so no SurfaceView is needed. The user knows that it's using the camera. I figured it out though.
Eclipsed4utoo
A: 

Ok, so I figured this out, I think.

If I make a static class object for the widget class, it seems to keep it in memory.

private static Camera camera = null;

Then setting it in code..

camera = Camera.open();

does seem to keep it in memory for use later. I don't know if that's really inefficient or not, but that's the only way I could get it to work.

Eclipsed4utoo