Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Is it possible to take a picture with the camera from a service with no UI [duplicate]

Tags:

android

camera

The only code I have been able to get working to take a picture with the camera runs from an activity. I am fairly certain that it is possible to take a photo from within a service, or from an AsyncTask launched by the service.

It seems to me that the camera API needs a SurfaceView which must be tied into a UI. Maybe I am wrong. Has anyone written code where a photo can be taken from a service?

like image 723
Johann Avatar asked Jan 11 '13 12:01

Johann


People also ask

How do I open gallery on Android?

From the Home screen, tap Apps > Gallery . Open Gallery from the Camera application by tapping the thumbnail image in the lower right corner of the screen.


1 Answers

It's possible by using WindowManager in Android.

https://stackoverflow.com/a/10268650/3047840

I should say yes you need SurfaceView to take a picture but it does not have to be tied to a certain xml layout. You can add it to the WindowManager class which works even in the Service. So the WindowManager here opens a 'window' for you to do any floating effect in Android.

Specifically,

You can add a SurfaceView into the WindowManager and set the parameters as following.

mPreview = new CameraPreview(this, mCamera, jpegCallback);
WindowManager wm = (WindowManager) this
        .getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
        PixelFormat.TRANSPARENT);

params.height = 1;
params.width = 1;

wm.addView(mPreview, params);

Android has this functionality while iOS doesn't. This is why you can have Facebook chathead active on the home screen in Android rather than iOS.

Show facebook like chat head from a broadcast receiver in android

like image 92
Fanglin Avatar answered Sep 27 '22 19:09

Fanglin