Android: BroadcastReceiver intent to Detect Camera Photo Taken?
Asked Answered
B

3

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?

Bohlin answered 8/12, 2010 at 15:52 Comment(0)
N
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.

Neeley answered 8/12, 2010 at 16:31 Comment(15)
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)?Bohlin
@JToland: There is no broadcast to say "the camera took a picture".Neeley
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?Bohlin
@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".Neeley
Great! That seems to be catching it now! I ended up using a FileObserver on the directory of interest. Thanks a lot!Bohlin
@Neeley 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 question for sms receive broadcast.Teeth
@Krishnabhadra: No, other than by having your activity take over the foreground.Neeley
@Neeley can you pleas look at this question #13239145Normi
@Neeley 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.Monasticism
@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.Neeley
@Neeley 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.Monasticism
@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.Neeley
@Neeley 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?Monasticism
@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.Neeley
let us continue this discussion in chatMonasticism
P
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

Piteous answered 19/7, 2016 at 11:32 Comment(2)
Note the "android:enabled="false""!Yttriferous
Deprecated and doesn't work in api level 24: developer.android.com/topic/performance/…Sterling
G
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>
Garretson answered 5/6, 2012 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.