Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android share image URL with Facebook SharePhotoContent

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!

like image 600
Lady_ari Avatar asked Apr 03 '15 02:04

Lady_ari


3 Answers

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);
    }
}
like image 184
rod_torres Avatar answered Oct 20 '22 10:10

rod_torres


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();
like image 36
Heshan Sandeepa Avatar answered Oct 20 '22 11:10

Heshan Sandeepa


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);
like image 1
junior developper Avatar answered Oct 20 '22 10:10

junior developper