May 27, 2013

Sharing the taken picture - Instant Mustache #9

This article is part of a series of articles about the development process of Instant Mustache, a fun camera app that adds mustaches to all faces using face detection. Click here to get a chronological list of all published articles about Instant Mustache.

Up to now our app can take and view pictures. The next step is to share the taken picture with other Android apps. This is done via Intents. The Intent system is one of the most powerful features of Android. It allows us to interact with any app that accepts images with almost no extra afford.

We could create an ActionBar item and when clicked launch an Intent to share the image but instead we are going to use a ShareActionProvider. The ShareActionProvider adds a share icon to the ActionBar as well as the icon of the app that the user has shared pictures the most with. By clicking this icon the user can share directly with this app. In addition to that the ShareActionProvider shows a sub menu with more apps that the given picture can be shared with.

A ShareActionProvider with Google+ as default share action.

Sub menu of a ShareActionProvider.

If sharing is a key feature of your activity, you should consider using the ShareActionProvider.

We start by creating an XML menu file for adding the share action. For legacy reasons the ActionBar uses the same approach for creating action items as the menu in Android 2.x.

activity_photo.xml

Once we inflated the menu in onCreateOptionsMenu() we need to set the Intent used to share the photo.

PhotoActivity.initializeShareAction()

Let's take a look at the different components of the Intent:
  • ACTION_SEND: The default action used for “sending” data to an other unspecified activity.
  • MIME type: The MIME type of the data being sent. Other apps can define multiple MIME types they accept. We are sending a JPEG image and therefore we are using the MIME type “image/jpeg”. To learn more about MIME types start with the "Internet media type" Wikipedia article.
  • EXTRA_STREAM: Uri that points to the data that should be sent. In our case the Uri is pointing to the image file on the external storage.

That's it already. For all changes done to the code base, see the repository on GitHub. In the next article we'll polish some aspects of the app before we start implementing the Face detection feature.

No comments:

Post a Comment