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!
如果您使用的是链接而不是本地图片,则必须使用ShareDialog而不是SharePhoto
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("title")
.setContentDescription("description")
.setContentUrl(Uri.parse("your link"))
.build();
我所做的是下载图像并从服务器获取位图,然后发布到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);
}
}
如果你想分享一个图像的URL,那么你应该使用sharelinkcontent
:这是我的代码,它的工作,但没有标题或描述只是图像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);
链接地址: http://www.djcxy.com/p/25338.html
上一篇: Grails 3没有包装?