8

I'm trying to share a photo using Facebook's new SharePhoto & SharePhotoContent classes in the new SDK. I'd like to use an image URL instead of a locally stored image.

I am able to share an image using a locally stored drawable resource:

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.carrots);

SharePhoto photo = new SharePhoto.Builder()
                   .setImageBitmap(image)
                   .build();

SharePhotoContent content = new SharePhotoContent.Builder()
                                .addPhoto(photo)
                                .build();

shareDialog.show(content);

But when I try to use an image URL :

SharePhoto photo = new SharePhoto.Builder()
                       .setImageUrl(Uri.parse("http://s3-ak.buzzfed.com/static/images/public/verticals/food-title.png?v=201504021353"))
                       .build();

all I'm seeing is a blank post in the Facebook shareDialog.

Has anyone else tried this and gotten it to work?

Many thanks!

Lady_ari
  • 433
  • 3
  • 8
  • 19
  • Interesting, when I use your exact code to create a SharePhoto, I see this error instead: http://i.imgur.com/O7TFGsM.jpg. Are you sure the Uri.parse is actually giving your a non-null Uri back? Sometimes the Uri.parse() method will fail silently, and just give you a null. – Ming Li Apr 03 '15 at 19:56
  • @MingLi I haven't seen that error message at all during testing. And I'm positive that the Uri is non-null. – Lady_ari Apr 03 '15 at 20:18
  • Are you using that exact url in your post above? If so, then you should file a bug at https://developers.facebook.com/bugs with a small sample app as a zip, then we can take a look at what's going on. – Ming Li Apr 03 '15 at 22:24
  • @MingLi I'm using that exact url. It will take me a bit to file that bug, since I'm on a tight deadline at work. So as a work-around, I've simply downloaded the image before sharing it as a bitmap. But thank you for your help! – Lady_ari Apr 03 '15 at 22:35
  • Did you solve your problem ? I have the same issue – Fayçal May 23 '15 at 16:16
  • @Back Packer no I'm still just downloading the image before sharing. – Lady_ari May 23 '15 at 16:46
  • Insane, 4 years have passed and facebook still has broken SDK. Struggling with the exact same problem for a week. Bitmap is not an option for me, because they save it as JPEG which doesn't preserve the transparent background. – Viktoriia Chebotar Aug 01 '19 at 17:27
  • Is it possible to share images provided by contentProvider, as in the images with `content://` uri using `SharePhoto`. – Shubham Naik Oct 07 '19 at 13:14

3 Answers3

3

What I did was download the image and get the bitmap from the server and then post to FB:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FacebookSdk.sdkInitialize(getApplicationContext());

    Button btn = (Button)findViewById(R.id.btn_share);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getImage();
        }
    });
}

private void getImage(){
    new DownloadImgTask().execute("http://url.com/1.jpg");
}

private void postFB(Bitmap bm){
    SharePhoto photo = new SharePhoto.Builder().setBitmap(bm).build();
    SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(photo).build();
    ShareDialog dialog = new ShareDialog(this);
    if (dialog.canShow(SharePhotoContent.class)){
        dialog.show(content);
    }
    else{
        Log.d("Activity", "you cannot share photos :(");
    }

}

private class DownloadImgTask extends AsyncTask<String, Void, Bitmap>{

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap bm = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            bm = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return bm;
    }

    protected void onPostExecute(Bitmap result) {
        postFB(result);
    }
}
rod_torres
  • 336
  • 2
  • 9
1

If you are using link instead of local image, you have to use ShareDialog instead of SharePhoto , From the doc

ShareLinkContent linkContent = new ShareLinkContent.Builder()
        .setContentTitle("title")
        .setContentDescription("description")
        .setContentUrl(Uri.parse("your link"))
        .build();
Heshan Sandeepa
  • 3,388
  • 2
  • 35
  • 45
  • So I can't just use a url to simply display a photo like it does with a Bitmap? And if not, then what's the point of the `setImageUrl` function in the SharePhoto model? – Lady_ari Apr 03 '15 at 05:41
  • Can you try with SharePhoto.Builder() photo = new SharePhoto.Builder() – Heshan Sandeepa Apr 03 '15 at 06:00
  • what you tried was "SharePhoto photo" not "SharePhoto.Builder photo " – Heshan Sandeepa Apr 03 '15 at 06:37
  • I don't think that will work. In the `SharePhotoContent` builder, I have to add a SharePhoto object, not a SharePhoto.Builder object. – Lady_ari Apr 03 '15 at 15:26
  • It will not work any more as setContentTitle, setContentDescription, setContentUrl are deprecated.https://developers.facebook.com/docs/sharing/android – Amardeep Aug 03 '17 at 09:22
1

If you want to share a URL of an image, then you should use sharelinkcontent: this is my code and it work but without an title or description just the image url:

    ShareLinkContent linkContent = new ShareLinkContent.Builder()
            .setContentTitle("Shared from nearbyme application")
            .setContentDescription("This is a wonderful place")
            .setContentUrl(Uri.parse("http://www.villathena.com/images/nearby/thumbs/le-bus-bleu-private-tours.jpg"))
            .setImageUrl(Uri.parse("http://www.villathena.com/images/nearby/thumbs/le-bus-bleu-private-tours.jpg"))
            .build();
      shareDialog.show(content);
Tushar Gogna
  • 4,963
  • 6
  • 31
  • 42