Android: Add your application to the "Share" menu

Photo gallery > Share > Your App.


That'd be nice aye? Luckily, it's not that hard coz I've got sharing images or files using Intents working on my Mustachify Everything app quite nicely.

A couple of tweaks required though. First, indicate that your app is willing to deal with image files coming from the "SEND" action.

Using the Eclipse Android SDK, the AndroidManfest.xml config should look a little something like this:


  • Add an intent filter
  • Add an action with the value of "android.intent.action.SEND"
  • Add data with the value of "image/*" for mime type
  • Add a category with the value of "android.intent.category.DEFAULT"

In XML, it looks like this:

<action android:name="android.intent.action.SEND" />
<data android:mimeType="image/*" />
<category android:name="android.intent.category.DEFAULT" />

That's the initial setup done.

Now to detect when your Activity was started by a sharing intent.

Sometime during Activity.onCreate(), we need to add some code to handle the incoming data:

Intent intent = getIntent();
Bundle extras = intent.getExtras();
String action = intent.getAction();

// if this is from the share menu
if (Intent.ACTION_SEND.equals(action)) { if (extras.containsKey(Intent.EXTRA_STREAM)) {
// Get resource path
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
String filename = parseUriToFilename(uri);

if (filename != null) {
moustachify(filename, null);

This is the main gist of the logic required to get sharing working.

Now the magic contained in parseUriToFilename() converts the image path information into a usable file path and name. That's the hard part.

The code below is a cleaned up version of mad's solution.

public String parseUriToFilename(Uri uri) {
String selectedImagePath = null;
String filemanagerPath = uri.getPath();

String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);

if (cursor != null) {
// Here you will get a null pointer if cursor is null
// This can be if you used OI file manager for picking the media
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
selectedImagePath = cursor.getString(column_index);

if (selectedImagePath != null) {
return selectedImagePath;
else if (filemanagerPath != null) {
return filemanagerPath;
return null;

Now that you've got the absolute filename for the file shared, you can do whatever the heck you want with it.

Except this...


Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog