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