30

I'm using Intent for sharing url and subject. In this intent filter showing all the sharing apps. i want only (facebook/gmail/message/skype/twitter) these option in popup. is this possible to customize sharing intent filter.

 Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
 sharingIntent.setType("text/plain");
 String shareBody = adapter.getDetails("url";
 sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"subject");
 sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
 startActivity(Intent.createChooser(sharingIntent, "Share via"));

thanks

sidon
  • 1,434
  • 1
  • 17
  • 30
Jeeva123
  • 1,047
  • 3
  • 20
  • 34

4 Answers4

72

Yes, its possible Check out below which shows the filteration for Facebook,Gmail,Twitter.

Updated to Share Text + Image:

Select the image from the SDCard:

String fileName = "image-3116.jpg";
String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
String myDir = externalStorageDirectory + "/saved_images/"; // the
            // file will be in saved_images
Uri uri = Uri.parse("file:///" + myDir + fileName);

Share via Twitter

  Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
   shareIntent.setType("text/plain");
   shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
   shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

   PackageManager pm = v.getContext().getPackageManager();
   List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
     for (final ResolveInfo app : activityList) 
      {
        if ("com.twitter.android.PostActivity".equals(app.activityInfo.name))
          {
             final ActivityInfo activity = app.activityInfo;
             final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
             shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
             shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
             shareIntent.setComponent(name);
             v.getContext().startActivity(shareIntent);
            break;
          }
        }

Share via Facebook

   Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
   shareIntent.setType("text/plain");
   shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
   shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
   shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

   PackageManager pm = v.getContext().getPackageManager();
   List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
     for (final ResolveInfo app : activityList) 
     {
         if ((app.activityInfo.name).startsWith("com.facebook.katana")) 
         {
           final ActivityInfo activity = app.activityInfo;
           final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
          shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
          shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
          shareIntent.setComponent(name);
          v.getContext().startActivity(shareIntent);
          break;
        }
      }

Share via Gmail

  Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
  shareIntent.setType("text/plain");
  shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
  shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

   PackageManager pm = v.getContext().getPackageManager();
   List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
       for (final ResolveInfo app : activityList) 
        {
          if ((app.activityInfo.name).contains("android.gm")) 
           {
             final ActivityInfo activity = app.activityInfo;
             final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
            shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
             shareIntent.setComponent(name);
             v.getContext().startActivity(shareIntent);
             break;
           }
       }

Share via WhatsApp:

 Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
 shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
        for (final ResolveInfo app : activityList) {
            if ((app.activityInfo.name).contains("com.whatsapp")) {
                    final ActivityInfo activity = app.activityInfo;
                    final ComponentName name = new ComponentName(
                                activity.applicationInfo.packageName, activity.name);
                          shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                        shareIntent.setComponent(name);
                        v.getContext().startActivity(shareIntent);
                        break;
                    }
                }
Alejandro Luengo
  • 1,256
  • 1
  • 17
  • 27
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • 6
    Facebook doesn't accept EXTRA_PARAMETERS anymore. https://developers.facebook.com/x/bugs/332619626816423/ – culebrins Mar 25 '14 at 15:16
  • 1
    for (final ResolveInfo app : activityList) not resolve for facebook, its display Type mismatch: cannot convert from element type Object to ResolveInfo – Hitesh Dhamshaniya May 02 '14 at 08:21
  • 3
    Great solution! There are only a few things that need to be improved. I implemented it only for Twitter, so I will tell only about twitter share. So firstly you need to give the type of the list: `List activityList = pm.queryIntentActivities(shareIntent, 0);` , and second - the activity name for twitter may be not exactly **com.twitter.android.PostActivity**, so it is dangerous to check with this, you'd better do like this: `if (app.activityInfo.name.contains("twitter")) {.....}` – Andranik May 17 '14 at 10:44
  • @andranik . I am already aware about it and o have implemented that way also. But I have provided general solution. Thanks for you suggestion . – GrIsHu May 18 '14 at 12:31
  • 10
    This doesn't work if user has Facebook app and Facebook messenger app installed. It may pick the wrong app. – Greg Ennis Aug 24 '14 at 22:22
  • Is there a way to differentiate between the two facebook apps? – Lion789 Aug 07 '15 at 00:01
  • 1
    @GregEnnis What pass of the "v.getTag" ?? which Object ?? – Hardik Parmar Aug 08 '15 at 05:45
  • i use com.facebook instead of "com.facebook.katana", this match the facebook and working fine. – Ankur1994a Apr 28 '16 at 13:12
  • 1
    @Ankur1994a That also can work because facebook would have changed its package name. – GrIsHu Apr 28 '16 at 13:21
16

Single method for multiple functionalities

Code of share(String nameApp,String imagePath,String message) function:

public void share(String nameApp, String imagePath, String message) {
    try {
        List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/jpeg");
        List<ResolveInfo> resInfo = getPackageManager()
                .queryIntentActivities(share, 0);
        if (!resInfo.isEmpty()) {
            for (ResolveInfo info : resInfo) {
                Intent targetedShare = new Intent(
                        android.content.Intent.ACTION_SEND);
                targetedShare.setType("image/jpeg"); // put here your mime
                                                        // type
                if (info.activityInfo.packageName.toLowerCase().contains(
                        nameApp)
                        || info.activityInfo.name.toLowerCase().contains(
                                nameApp)) {
                    targetedShare.putExtra(Intent.EXTRA_SUBJECT,
                            "Sample Photo");
                    targetedShare.putExtra(Intent.EXTRA_TEXT, message);
                    targetedShare.putExtra(Intent.EXTRA_STREAM,
                            Uri.fromFile(new File(imagePath)));
                    targetedShare.setPackage(info.activityInfo.packageName);
                    targetedShareIntents.add(targetedShare);
                }
            }
            Intent chooserIntent = Intent.createChooser(
                    targetedShareIntents.remove(0), "Select app to share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    targetedShareIntents.toArray(new Parcelable[] {}));
            startActivity(chooserIntent);
        }
    } catch (Exception e) {
        Log.v("VM",
                "Exception while sending image on" + nameApp + " "
                        + e.getMessage());
    }
}

For attaching image on gmail, facebook, twitter with text use below code.

File filePath = new File("your image path");
share("gmail", filePath.toString(),"your comment");
share("facebook", filePath.toString(),"your comment");
share("twitter", filePath.toString(),"your comment");
Aks
  • 8,181
  • 5
  • 37
  • 38
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • is it possible to share on google+ and linkedIn with same code? if not what changes has to be done – Anitha Feb 04 '15 at 12:05
2

Above given answer open facebook chat app on my phone. For the following worked !!

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                sharingIntent.putExtra(Intent.EXTRA_TEXT, "Text...");
                PackageManager packManager = getPackageManager();
                List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(sharingIntent,  PackageManager.MATCH_DEFAULT_ONLY);

                boolean resolved = false;
                for(ResolveInfo resolveInfo: resolvedInfoList){
                    if(resolveInfo.activityInfo.packageName.startsWith("com.facebook.katana")){
                        sharingIntent.setClassName(
                            resolveInfo.activityInfo.packageName, 
                            resolveInfo.activityInfo.name );
                        resolved = true;
                        break;
                    }
                }
                if(resolved){
                    startActivity(sharingIntent);
                }else{

                     Builder alert  = new AlertDialog.Builder(ActivityName.this);
                        alert.setTitle("Warning");
                        alert.setMessage("Facebook App not found");
                        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int which) 
                            {
                                dialog.dismiss();

                            }
                        });
                        alert.show();
                } 
arun
  • 245
  • 1
  • 3
  • 12
  • +1 for sharing! but i cant send any text/image.... i know this is a bug... but is there really any workaround without using the facebook sdk? – denvercoder9 Mar 31 '15 at 16:56
0

You can't share Extras with image without using facebook sdk.

https://developers.facebook.com/x/bugs/332619626816423/

Ahmed Mostafa
  • 918
  • 1
  • 12
  • 20