13

I was playing around with the new Google+ application on the Android and I had a technical question that I couldn't seem to find online. How does the instant upload hook into the native camera application?

I can turn on a feature in the Google+ application and any pictures that I take with the native camera app are automatically uploaded to a private album on Google+. And this seems to be true whether the Google+ application is currently running or not. Is there any type of hook that I can register a callback event for in my application to access "just-taken" pictures for some post-processing goodness? I'd really like to tap into this functionality for an application that I am currently writing.

Any insight or hints in the right direction are greatly appreciated! Thanks!

Kara
  • 6,115
  • 16
  • 50
  • 57
  • You may want to look at this: http://code.google.com/p/android/issues/detail?id=1480 – James Black Jul 09 '11 at 22:25
  • 1
    That is more focused on launching the camera intent and getting data back. I would really like to passively monitor the camera application or use some type of callback like the Google+ application seems to do. –  Jul 10 '11 at 04:02
  • 1
    See a similar post: http://stackoverflow.com/questions/230643/android-api-for-detecting-new-media-from-inbuilt-camera-mic – AlikElzin-kilaka Jul 19 '12 at 16:08

2 Answers2

12

Not entirely sure exactly how Google+ does it, but one way that would work is to use the ContentObserver interface: grab the MediaStore.Images.Media ContentProvider and attach a ContentObserver to it. Anytime a camera image is added, you'll get a notification and you can process it accordingly (including uploading).

Same technique would work for just about any ContentProvider that properly handles ContentObservers (which I'm assuming is all ContentProviders, but you never can tell).

Femi
  • 64,273
  • 8
  • 118
  • 148
  • @John @Femi that would be my approach as well, it would then work for any camera app that put it's images in the media store (or actually anywhere on the sdcard since media store scans that for new images surprisingly, though it might take a while to register those ) – Idistic Jul 10 '11 at 04:34
  • This works great... now just a few other technical questions that I need answered and we should be good to go! Thanks! –  Jul 21 '11 at 03:19
  • 2
    **Does this work even if the app was killed by the system? My guess is no**, because it's code based and not manifest based. Very different than xml based broadcast receiver which wakes up the app when a broadcast is initiated. If I'm right, this is kinda useless unless you manage to keep your app running all the time, which is not nice. Hope I'm wrong. – AlikElzin-kilaka Jul 16 '12 at 09:47
  • Regretfully, I'm probably right. Quoting from http://developer.android.com/guide/topics/providers/content-providers.html: *connects data in one process with code running in another process* – AlikElzin-kilaka Jul 16 '12 at 11:10
  • **Now I'm totally sure. I simulated the app being stopped by the system and the ContentObserver was not invoked** – AlikElzin-kilaka Jul 16 '12 at 11:48
  • This is disturbing because, either Google are using some undocumented broadcast receiver, or more probably, they keep their app **running all the time** and listening to the filesystem or the content resolver - either is bad and not playing nice. – AlikElzin-kilaka Jul 16 '12 at 11:49
  • Hi Femi, I have a question on this topic, how does gallery gets update on any new image file being moved/copy/created on device internal/external memory ? I have a similar requirement to track for any imagefile being moved/created on device based on the path it is getting created. Any input regarding the same will help me a lot. -Thanks, Manju – Manju Aug 17 '13 at 10:08
  • Honestly not quite sure: you may want to look at the source for the Gallery app. You can see an old one at https://github.com/CyanogenMod/android_packages_apps_Gallery . – Femi Aug 17 '13 at 17:46
  • Looks like an undocumented broadcast does exist, at least according to this Github issue. Try creating a manifest receiver for the action "com.android.camera.NEW_PICTURE". Source: https://github.com/dichro/Fe-Fi/pull/1 – ian.shaun.thomas Oct 09 '13 at 20:00
7

Thanks to Femi's answer in this post, I was able to take what he said and figure out how to emulate Google+'s Instant Upload feature. I wrote a tutorial about it here:

http://www.jessechen.net/blog/how-does-google-plus-instant-upload-work/

Jesse Chen
  • 4,928
  • 1
  • 20
  • 20
  • I'm using your code but onChange method is being called for more than one time for one picture taken and also it's being called if any picture is deleted from gallery, I just want to know have you handled these things in your hackathon? If yes how you did that? – Waqas Apr 07 '13 at 17:33
  • 1
    @waqas716 this is a known problem with contentObservers in general. you will just have to keep track of what's going on and ignore events that don't match your needs. for example, in order to check which new images were added, get only images that were created after the timestamp you store from the previous queries. – android developer Feb 02 '14 at 09:04