5

How i can share a image from from my app to facebook in android?

mohammedsuhail
  • 701
  • 2
  • 11
  • 23

3 Answers3

6
public void shareOnFacebook() {
    Uri uri = Uri.parse("http://m.facebook.com/sharer.php?u=" +
            website_url + "&t="+ someTitle.replaceAll(" ","+");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}

where your website should hold a meta tag like

<link rel="image_src" type="image/jpeg" href="http://www.domain.com/someimage.jpg" />

which will then be used on Facebook as the promo image. Of course this can also be done dynamically on server side.

This is the approach where you use an image that's also on a server somewhere already, so you don't need to send it from your Android device to a server first. Depending on your use case, but this is usually the easiest way.

Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192
2

You could achieve this using the Facebook SDK for Android.

A better way is to allow your user to share images using a service of their choice. Android can automatically present a list of apps which can handle images for you. Simply state that you have an image to send somewhere:

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/path/to/image.ext"));

startActivity(Intent.createChooser(share, "Share Image"));
David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
  • if i add Intent.EXTRA_TEXT to it, text is never posted to facebook. how can i resolve this issue please help me out. – varun bhardwaj Jan 18 '12 at 10:25
  • @David Caunt I tried this but this is not working for me image never gets posted on fb using this code .. please suggest something. – Shruti Jun 18 '12 at 08:25
  • What if I have my line is like this: `file = new File(folder + "/pb_image_" + Math.random() + ".png");` do I still have to use the `Uri.parse()`? – Si8 Aug 30 '13 at 17:51
  • @David Snabel-Caunt - I want to share an image to WorkPlace(facebook's) on selected group? – Arnold Brown Jul 10 '18 at 07:02
0

Try to use Facebook SDK for android

private void sharePhotoToFacebook() {

    byte[] byteArray = getIntent().getByteArrayExtra("image");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

    SharePhoto photo = new SharePhoto.Builder()
            .setBitmap(bmp).setCaption("This is a text")
            .build();
    SharePhotoContent content = new SharePhotoContent.Builder()
            .addPhoto(photo)
            .build();
    ShareApi.share(content, null);
}

See here for more details on sharing

ekad
  • 14,436
  • 26
  • 44
  • 46
Selvam
  • 1
  • 3