Monday 24 December 2012

Share Image with text on Twitter , Facebook and Email etc. in Android

Hi folks, I am here sharing a code which you can use in your project to share the text with image on Twitter, Facebook, Email.

But in case of facebook you can not share the text on facebook using Intent but you can share the link on facebook and rest twitter/email you can share the text plus image perfectly.

If you are looking to share the text not link then in case of facebook you have to use the Facebook SDK.

1) For attaching image on gmail with text use below code.

File filePath = context.getFileStreamPath("vmphoto.jpg");
share("gmail",filePath.toString());

2) For sharing image on facebook

File filePath = context.getFileStreamPath("vmphoto.jpg");
share("facebook",filePath.toString());

3) For sharing image with text on twitter

File filePath = context.getFileStreamPath("vmphoto.jpg");
share("com.twitter.android",filePath.toString());

/**
* To share photo with text on facebook , twitter and email etc.@param nameApp
* @param nameApp
* @param imagePath
* /

void share(String nameApp, String imagePath) {
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,"This photo is created by App Name");
                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());
 }
}

12 comments:

  1. This example is very good. But for me, in twitter sharing, it not share the image...do you test it?

    ReplyDelete
  2. Yes I tested it well on tablet and phone both. Its working and sharing the photo on twitter, facebook, email. Please go through it once again and let me know if you have still same issue and don't forget to post the Exception message.

    ReplyDelete
    Replies
    1. Hi This works fine for twitter but somehow not working for LINE..
      For line it posts only image not text. I don't know why it is filtering the text.

      Delete
  3. Hi. Thank you for your code. Unfortunately, it doesn't work for me. Can you help please.

    I'm trying to capture a screenshot of an activity in android, then post it on facebook. Here is my code:
    //////////////////////////start of my code////////////////////////////

    //"screen" is my xml layout for an activity
    View screen = (View) findViewById(R.id.screen);

    View rootview = screen.getRootView();
    rootview.setDrawingCacheEnabled(true);
    Bitmap bitmap = rootview.getDrawingCache();

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
    File file = new File(Environment.getExternalStorageDirectory() + /captured_screen.jpg");

    try
    {
    file.createNewFile();
    FileOutputStream ostream = new FileOutputStream(file);
    ostream.write(bytes.toByteArray());
    ostream.close();
    }

    catch (Exception e){
    e.printStackTrace();
    }

    ContextWrapper context = new ContextWrapper(getApplicationContext());
    File filePath = context.getFileStreamPath("captured_screen.jpg");
    share("facebook",filePath.toString()); //your function

    //////////////////end of my code/////////////

    The screen capture is definitely working because If I set my ImageView to the "bitmap" variable, the screen capture is displayed in my app.

    However, when I attempt to use the share() function, it allows me to sign in into facebook but I get an error saying "An error occurred while uploading the photo". Is there any way you can help me.

    Thanks

    ReplyDelete
    Replies
    1. Also, when I use "gmail" instead of facebook, the email is sent but no attachment is included.

      Delete
  4. will you provide me zip file because i am beginner in android and i want to share a image from which come through web-services i want to share it i don't want to share a image via sd cards

    ReplyDelete
  5. I can't post image. My image can't display. Help me, please !

    ReplyDelete
    Replies
    1. Please confirm that you are getting bitmap correctly & debug it, may be something is null in your case & last let me know from where you are tying to pick bitmap.

      Delete
    2. I code by libgdx. I save my image by code in folder core. Now, I transfer image's path to method in folder android. I check it image in folder android and it's exist. But I call method facebook, it isn't display only text. Do you have skype ? My skype: minhduc_idtech90

      Delete
  6. I can't see text with image only image is posted

    ReplyDelete