0

I am implementing share in my app. Sharing mixed content like image+text is possible with some apps like whatsapp, twitter etc. while facebook, linkedin etc. only allow content of one type(either image or text). How can this be achieved? E.g when whatsapp is selected it should send both, whereas when facebook is selected it should share text. One solution I thought of querying all the activities that can perform this action, then creating target intent. Below is what I have tried so far

Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
    shareIntent.putExtra(Intent.EXTRA_TEXT, title + "\n" + message);
    List<ResolveInfo> resolveInfos = activity.getApplicationContext().getPackageManager().queryIntentActivities(shareIntent, PackageManager.MATCH_DEFAULT_ONLY);
    ArrayList<Intent> mainIntentList = new ArrayList();
    Iterator<ResolveInfo> it = resolveInfos.iterator();
    while (it.hasNext()) {
        ResolveInfo localResolveInfo = it.next();
        String str = localResolveInfo.activityInfo.packageName;
        if (imageSupportingApps.contains(str) && (imageUri != null)) {
            Intent localIntent = new Intent(Intent.ACTION_SEND);
            localIntent.setComponent(new ComponentName(str, localResolveInfo.activityInfo.name));
            localIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
            localIntent.setType("image/*");
            localIntent.putExtra(Intent.EXTRA_SUBJECT, title);
            localIntent.putExtra(Intent.EXTRA_TEXT, title + "\n" + message);
            localIntent.setPackage(str);
            mainIntentList.add(localIntent);
        }
    }
    Intent in = Intent.createChooser(shareIntent, "Share on ..");
    in.putExtra(Intent.EXTRA_INITIAL_INTENTS, mainIntentList.toArray(new Parcelable[localArrayList1.size()]));
    activity.startActivity(in);

Update 1: This is what I tried so far. But it is only sending text. It is not sharing image with whatsapp.

Nitish
  • 3,097
  • 13
  • 45
  • 80
  • Your `shareIntent` is for type `text/plain` so the system only returns matches for that MIME type. The MIME type of the share must be `image/*` or similar. If you want to filter more ask for `*/*` and sift through the results. – escape-llc Feb 09 '17 at 13:51
  • @escape-llc As I have mentioned in my post above, apps like facebook or linkedin do not allow mixed content to be shared. If you try to share image+text then they will pick only image and ignore the text content. Whereas whatsapp allows mixed content. That's why I cannot use `*/*`. There are apps that are doing so, I mean with facebook they share text only, whereas with whatsapp they share mixed content. I am trying to achieve the same.:) – Nitish Feb 10 '17 at 04:49

0 Answers0