nbarraille
nbarraille

Reputation: 10023

Take a picture from an Android Activity "as a Service"

I want to take a picture from a non-activity Thread (a web server running on the device).

In order to do this, I wanted to have a Service bound to the Application, that I could call which would take a picture, and return the Bitmap when done. But it looks like I can't do that because for security purposes Android forbids to take picture without showing a preview (even with a dummy preview like shown on the accepted answer here, on some devices)

I then wanted to have an Activity that takes the picture for me, with a real preview, but I can't think of any way to do this either because:

How would you do something like that in the most logical/elegant way possible?

Upvotes: 2

Views: 1217

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007533

I sincerely hope that this is for personal use only, and that you are not seriously considering putting a Web server -- with an open, unprotected server socket -- on lots of mobile devices. If you are, I hope you have a well-financed legal defense fund.

That being said:

Step #1: Have your Web daemon service register a BroadcastReceiver with LocalBroadcastManager

Step #2: As needed, have your service call startActivity() to bring up your picture-taking activity

Step #3: Have your picture-taking activity send a broadcast through LocalBroadcastManager for your Web daemon service, with the JPEG data either in an Intent extra, or perhaps just as a static data member if you're sure you will be able to process it before taking the next image.

The use of LocalBroadcastManager avoids shipping the Intent (and its huge bitmap extra) to the OS and back to your process again.

Upvotes: 2

Related Questions