0

I want to share image along with caption in all possible app including facebook. I tried this code,it is working for all the other apps except facebook.Can anyone please help me how to share the caption along with image in facebook.

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
    shareIntent.putExtra(Intent.EXTRA_TEXT, caption);
    shareIntent.setType("image/*");
    mcontext.startActivity(Intent.createChooser(shareIntent, "Share Image")); 
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
Ragamai K
  • 3
  • 4

2 Answers2

0

Try this.

Store your image in external storage directory and take path of that image and for text use android.content.Intent.EXTRA_TEXT and pass your static text.

String fileName = "imagename.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 intentShare = new Intent(android.content.Intent.ACTION_SEND);
intentShare.setType("text/plain");
intentShare.putExtra(android.content.Intent.EXTRA_SUBJECT, "Testing Subject");
intentShare.putExtra(android.content.Intent.EXTRA_TEXT, "Testing Texts");
intentShare.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;
        }
      }

Include this permissions in your manifest.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • Check i have edited my answer. do it using package manager class i have added. – Jay Rathod May 07 '16 at 08:01
  • If I use "com.facebook.katana" then it allows to share only for the facebook app not for the other apps – Ragamai K May 07 '16 at 08:10
  • Yes `intent send` would not share and allow it directly to facebook. it will do it for all other but for facebook you need to use it's package for identification. only way to do it with this or else you may use `Facebook SDK`. – Jay Rathod May 07 '16 at 08:18
  • @RagamaiK Check this link and answer given here http://stackoverflow.com/questions/9730243/how-to-filter-specific-apps-for-action-send-intent-and-set-a-different-text-for – Jay Rathod May 07 '16 at 08:45
-1
public void onShareClick(View v){
    List<Intent> targetShareIntents=new ArrayList<Intent>();
    Intent shareIntent=new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    List<ResolveInfo> resInfos=getPackageManager().queryIntentActivities(shareIntent, 0);
    if(!resInfos.isEmpty()){
        for(ResolveInfo resInfo : resInfos){
            String packageName=resInfo.activityInfo.packageName;
            Log.i("Package Name", packageName);
            if(packageName.contains("com.twitter.android") || packageName.contains("com.facebook.katana") ){
                Intent intent=new Intent();
                intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
                intent.setAction(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, "Text");
                intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                intent.setPackage(packageName);
                targetShareIntents.add(intent);
            }
        }
        if(!targetShareIntents.isEmpty()){
            Intent chooserIntent=Intent.createChooser(targetShareIntents.remove(0), "Choose app to share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
            startActivity(chooserIntent);
        }else{
            showDialaog(this);
        }
    }
}