Monday, 7 October 2013

Attaching images to Google+ shares on Android

After the recent iOS 1.4.0 SDK update that allowed sharing images straight to Google+ from within an application, a couple of people asked whether it was possible to do the same thing on Android. Luckily, it is, and it is very straightforward.

The PlusShare.Builder class has an addStream method which you can use to attach an image when configuring a share. Note that just like on iOS, you can't use it while attaching a URL, or as part of an interactive post. The method takes a URI, and will add it to the intent as a standard EXTRA_STREAM. There's also setStream, which overrides any previously attached streams.

All we do in the snippet below is trigger a picker intent to allow the user to choose an image from the gallery. We then create a share builder and add the returned URL. Note that this usage is not passing a PlusClient when creating the Builder, so it will work for users that aren't signed in with Google+. If your users are, then definitely use the version that accepts a PlusClient!

Obligatory Silvano screenshots: