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!
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);
}
}