Tuesday 28 April 2015

Sharing of Single & Multiple Images + Text using ACTION_SEND & ACTION_SEND_MULTIPLE Intent Services in AndroidMULTIPLE IMAGES + TEXT

Hello Friends,


    In android we can use the default share intent to share our image,text to an app installed in our device, but many times we only need specific application like Whatsapp, Facebook, Twitter, Gmail, etc. to be filtered out for sharing images + text. 

        Sometimes it happens that we want to share multiple images and text to share on a social apps like  Whatsapp, Facebook, Twitter, Gmail, etc. or may be on any application which are available in our device. 

So ,today i am going to show you the way to share SINGLE IMAGE + TEXT on a social media for particular application as well as all the social application in a device .

 And to share MULTIPLE IMAGES + TEXT on a social media for particular application as well as all the social application in a device .

So first of all i will show the code to share SINGLE IMAGE + TEXT on a social media.

Share single Image + Text using the Intent Services use the below code:
---------------------------------------------------------------------------------------------------------
       //Select a file from sdcard to share
       String fileName = "image-3116.jpg";//Name of an image
       String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
       String myDir = externalStorageDirectory + "/saved_images/"; // the file will be in saved_images
       Uri uri = Uri.parse("file:///" + myDir + fileName);

        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);
        startActivity(Intent.createChooser(shareIntent, "Share Deal"));

=====================================================================
To share the Image + Text on Single application like Facebook,Gmail,WhatsApp,Twitter for that below is the method 
=====================================================================

Select the image from the SDCard and convert it into an URI: 
---------------------------------------------------------------------------------------------------


 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).contains("facebook")) 
             {
               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;
    }
    }



+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Now if you want to share multiple Images using Intent Services use the below code which selects multiple images from one of the folder from the sdcard and adds images into an ArrayList<Uri> and pass it into the stream: 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 //Select one folder from the sdcard and get all the images from it.
String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();

// the files will be in saved_images folder. You can change it according to your folder name.
String myDir = externalStorageDirectory + "/saved_images/";


 //Filter only images from the folder.
FilenameFilter m_filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
if (filename.endsWith(".jpeg") || filename.endsWith(".jpg")|| filename.endsWith(".png")) {
return true;
        }
        return false;
}
};
ArrayList<Uri> Imageuris = new ArrayList<Uri>();
File m_file = new File(myDir);
if (m_file.isDirectory()) {
for (File f : m_file.listFiles(m_filter)) {
Uri uri = Uri.parse("file:///" + f.getAbsolutePath().toString());
Imageuris.add(uri);
}
}


++++++++++++++++++++++++++++++
    Share via Gmail:
++++++++++++++++++++++++++++++

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "to" });
shareIntent.putExtra(Intent.EXTRA_STREAM, Imageuris);
  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));
                       
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 Facebook:
++++++++++++++++++++++++++++++

 Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "to" });
shareIntent.putExtra(Intent.EXTRA_STREAM, Imageuris);
  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));
                       
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("facebook")) {
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_MULTIPLE);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "to" });
shareIntent.putExtra(Intent.EXTRA_STREAM, Imageuris);
  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));
                       
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;
}
}


++++++++++++++++++++++++++++++
  Share via Twitter:
++++++++++++++++++++++++++++++

 Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "to" });
shareIntent.putExtra(Intent.EXTRA_STREAM, Imageuris);
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));
                       
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
  if((app.activityInfo.name).contains("com.twitter.android.PostActivity")) {
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;
     }
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Lastly to share multiple Images + Text on any Social media besides specifying any fix application you can use the below code which do not filters an application from the device and provides list of all the shareable apps from the device.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
  shareIntent.setType("text/html");
  shareIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "to" });
  shareIntent.putExtra(Intent.EXTRA_STREAM, Imageuris);
  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));
  startActivity(shareIntent);


Also you can help out to improve it more. 
I Hope this will help a lot !!

Enjoy!! 

No comments:

Post a Comment