Oct 10, 2011

Android Add menu in Share

Many times you might observe 3rd party application icon in Android Share menu. Suppose your application pick image/video from gallery, pick contact from contact book or pick audio from music player In this situation better options is to add your application menu in Share options so that user can directly select image/video from gallery or relavent application.

Share menu will display name & icon of your application

It is very easy to add menu in Share option.

Step 1 : Create Android project

Step 2 : Create activity named "MyShareMenu.java" (No need to create xml layout for this Activity)

Step 3: Register new activity in AndroidManiFest.xml file like following

<activity android:name="MyShareMenu">
                <action android:name="android.intent.action.SEND"></action>
                <data android:mimetype="*/*"></data>
                <category android:name="android.intent.category.DEFAULT"></category>
The importent part is data tag in that you can specify mimeType as per your application supports. For the demo purpose I have choose */* means every mimeType but you can write image/*, audio/* , video/* as per the need of your application.

Step 4: Fetch Uri of selected item, When user click on application from the Share menu, Android will call "MyShareMenu" activity with uri of selected item. Use following code to get Uri.

protected void onCreate(Bundle savedInstanceState) {
  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);
    Toast.makeText(this, "Selected File :" + uri.toString(), Toast.LENGTH_LONG).show();

In above code I have displays Toast but your can write business logic as per your need.