Android与Facebook SharePhotoContent共享图像URL
我试图在新SDK中使用Facebook的新SharePhoto和SharePhotoContent类共享一张照片。 我想使用图片网址而不是本地存储的图片。
我可以使用本地存储的可绘制资源来共享图像:
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);
但是,当我尝试使用图片网址时:
SharePhoto photo = new SharePhoto.Builder()
                       .setImageUrl(Uri.parse("http://s3-ak.buzzfed.com/static/images/public/verticals/food-title.png?v=201504021353"))
                       .build();
我看到的只是Facebook shareDialog中的一个空白帖子。
有没有其他人试过这个,并得到它的工作?
非常感谢!
如果您使用的是链接而不是本地图片,则必须使用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);
上一篇: Android share image URL with Facebook SharePhotoContent
