8

I'm working on an Android application which needs to perform an action each time a new image is taken with the phone. I don't want to take the image within my application, but rather perform some action when the Camera app takes an image and saves it to the SD card. Right now, I've gotten a BroadcastReceiver implemented that's currently listening for "android.intent.action.CAMERA_BUTTON". However, this doesn't seem to get called when I'm wanting it to. I've tried to debug the application on my own phone with a linebreak on the first line of the BroadcastReceiver's OnReceive method, but it never reaches that code.

Does anyone know what the correct intent for which I should be listening is? Or is using a BroadcastReceiver not even the best way to do this? (For example, is there a better way to accomplish this such as listening for when a new image is saved to the card)?

Thanks!

Update: I do have a Trackball on my phone (HTC Eris), so is it possible that taking photos that way doesn't get sent as "Camera Button"? If so, is there a workaround for a phone without a "Camera Button", but rather something like a Trackball?

JToland
  • 3,630
  • 12
  • 49
  • 70

3 Answers3

7

Right now, I've gotten a BroadcastReceiver implemented that's currently listening for "android.intent.action.CAMERA_BUTTON". However, this doesn't seem to get called when I'm wanting it to.

That only gets broadcast if the foreground activity does not consume the event.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So....then is it even possible to do what I'm wanting to do (because obviously I don't want to stop the camera from consuming the event)? – JToland Dec 09 '10 at 12:43
  • 2
    @JToland: There is no broadcast to say "the camera took a picture". – CommonsWare Dec 09 '10 at 14:01
  • BUT...is there a broadcast, that you know of, to say "a new photo (or 'file' if I can ask this pertaining to a specific directory) has been saved"...or something along those lines? – JToland Dec 09 '10 at 15:33
  • 9
    @JToland: You can set up a `ContentObserver` on the `MediaStore`. You can set up a `FileObserver` on some spot in the SD card. Neither are strictly "a new photo has been saved". – CommonsWare Dec 09 '10 at 15:38
  • Great! That seems to be catching it now! I ended up using a FileObserver on the directory of interest. Thanks a lot! – JToland Dec 09 '10 at 19:55
  • @CommonsWare That only gets broadcast if the foreground activity does not consume the event. any chance for making my activity or service the first receiver of the event, like you can register your receiver with intent filter that has a higher priority, just like the answer given in [this](http://stackoverflow.com/questions/1741628/can-we-delete-an-sms-in-android-before-it-reaches-the-inbox) question for sms receive broadcast. – Krishnabhadra Aug 08 '12 at 06:42
  • 2
    @Krishnabhadra: No, other than by having your activity take over the foreground. – CommonsWare Aug 08 '12 at 10:24
  • @CommonsWare can you pleas look at this question http://stackoverflow.com/questions/13238645/broadcastreceiver-dont-receive-keypress-on-camera-button – Viktor Apoyan Nov 05 '12 at 20:10
  • 1
    @CommonsWare How does one's app "take over the foreground" while the Camera app is in operation? Is monitoring the MediaStore the real only viable option if you need to track Camera usage? Would this also be the same for taking videos? Thanks. – gonzobrains Apr 29 '13 at 22:25
  • @gonzobrains: "How does one's app "take over the foreground" while the Camera app is in operation?" -- somehow call `startActivity()` on one of your activities. "Is monitoring the MediaStore the real only viable option if you need to track Camera usage?" -- since I have no idea what "Camera usage" really means, I can't answer that. – CommonsWare Apr 29 '13 at 22:27
  • @CommonsWare You previously answered JToland with the suggestion setting up ContentObserver on MediaStore. My goal is simply to determine when photos are taken. It would be great to have some sort of event along with details about the photo, but it doesn't look like this is so easy to do. – gonzobrains Apr 29 '13 at 22:43
  • 1
    @gonzobrains: "My goal is simply to determine when photos are taken" -- there are thousands of apps that take photos. They all can do different things with them, and you have no way to know when they take pictures or what they do with them. – CommonsWare Apr 29 '13 at 22:44
  • @CommonsWare True, but I think I can focus on just the built-in app for now if that were even possible. Is android.intent.action.CAMERA_BUTTON only used for the built-it camera app? If I only concerned myself with the native camera app, would you recommend your previous suggestion about monitoring the MediaStore? – gonzobrains Apr 29 '13 at 22:57
  • @gonzobrains: "True, but I think I can focus on just the built-in app for now" -- of which there are dozens. While any given device may only have one "built-in app", most device manufacturers have their own app, or perhaps a few apps for different generations of their devices. "Is android.intent.action.CAMERA_BUTTON only used for the built-it camera app?" -- there are few devices with a CAMERA button in the first place; what might all use it will vary by device. It's a broadcast if (and only if) the foreground app does not consume the event, and any app could listen for that broadcast. – CommonsWare Apr 29 '13 at 23:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29145/discussion-between-gonzobrains-and-commonsware) – gonzobrains Apr 29 '13 at 23:06
4

Quite an old question, if anyone still needs the solution try using

android.hardware.action.NEW_PICTURE

as your intent filter

<!-- Receiver for camera clicks -->
    <receiver
        android:name=".receivers.CameraEventReceiver"
        android:label="CameraEventReceiver">
        <intent-filter>

            <!-- <action android:name="com.android.camera.NEW_PICTURE" /> -->
            <action android:name="android.hardware.action.NEW_PICTURE" />

            <data android:mimeType="image/*" />
        </intent-filter>
    </receiver>

It will return the Uri of the image in intent.getData() of its onReceive() function

Hope it helps

sanket vetkoli
  • 826
  • 14
  • 18
0

try this one this works for me

<receiver android:name=".pictureReceiver" >
        <intent-filter android:priority="10000" >
            <action android:name="android.intent.action.CAMERA_BUTTON" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</receiver>
Chirag Patel
  • 11,416
  • 3
  • 26
  • 38