Android分享图片URL给Facebook SharePhotoContent

8
我将使用 Facebook 的新 SharePhoto 和 SharePhotoContent 类在新的 SDK 中翻译并分享一张图片。我想使用图片的 URL 而不是本地存储的图片。
我可以使用本地存储的 drawable 资源来分享一张图片:
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);

但是当我尝试使用图片URL时:
SharePhoto photo = new SharePhoto.Builder()
                       .setImageUrl(Uri.parse("http://s3-ak.buzzfed.com/static/images/public/verticals/food-title.png?v=201504021353"))
                       .build();

我在Facebook分享对话框中只看到一个空白帖子。

有人尝试过并成功了吗?

非常感谢!


@MingLi 在测试过程中,我根本没有看到过那个错误信息。而且我可以肯定 Uri 是非空的。 - Lady_ari
你在上面的帖子中使用了确切的URL吗?如果是这样,那么你应该在https://developers.facebook.com/bugs上提交一个小样本应用程序的zip文件来报告错误,然后我们可以查看发生了什么。 - Ming Li
你解决了你的问题吗?我也遇到了同样的问题。 - Fayçal
@Back Packer 不,我还在下载图片,等下载完再分享。 - Lady_ari
能否使用SharePhoto分享由contentProvider提供的图片,例如使用content:// URI的图片? - Shubham Naik
显示剩余3条评论
3个回答

3
我所做的是从服务器下载图像并获取位图,然后发布到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);
    }
}

是的,那也是我最终做的。 - Lady_ari

1
如果您使用的是链接而不是本地图片,您需要使用ShareDialog而不是SharePhoto,参见doc
ShareLinkContent linkContent = new ShareLinkContent.Builder()
        .setContentTitle("title")
        .setContentDescription("description")
        .setContentUrl(Uri.parse("your link"))
        .build();

那么我不能像使用位图一样使用URL来简单地显示照片吗?如果不行,那么SharePhoto模型中的“setImageUrl”函数有什么意义呢? - Lady_ari
你可以尝试使用SharePhoto.Builder() photo = new SharePhoto.Builder()。 - Heshan Sandeepa
你尝试的是“SharePhoto photo”,而不是“SharePhoto.Builder photo”。 - Heshan Sandeepa
我认为那样做不会起作用。在SharePhotoContent构建器中,我必须添加一个SharePhoto对象,而不是一个SharePhoto.Builder对象。 - Lady_ari
由于setContentTitle、setContentDescription和setContentUrl已被弃用,因此它将不再起作用。https://developers.facebook.com/docs/sharing/android - Amardeep

1
如果您想分享一张图片的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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接