如何在Facebook上与消息分享照片?

时间:2016-10-28 10:48:02

标签: android facebook sharing

我遇到了一个我认为应该简单的问题。我在活动上有两个图像按钮:一个是" rate&#34 ;;另一个是"分享"。我想要的是当一个人点击"分享"它将图像和消息发布到他们的时间线。

我已经集成了facebook sdk,所以这里是我尝试使用的代码:

public ImageButton imgbutton3, imgbutton2;
public CallbackManager callbackManager;
public ShareDialog shareDialog;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FacebookSdk.sdkInitialize(getApplicationContext());

    setContentView(R.layout.game_console);

    imgbutton3 = (ImageButton)findViewById(R.id.imageButton3);
    imgbutton3.setOnClickListener(ImgClickListener);
    imgbutton2 = (ImageButton)findViewById(R.id.imageButton2);
    imgbutton2.setOnClickListener(ImgClickListener);


}

 private View.OnClickListener ImgClickListener = new View.OnClickListener()      {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageButton3:
                //post to facebook...
                callbackManager = CallbackManager.Factory.create();
                shareDialog = new ShareDialog(this); //(stuck here! :( )



                break;
            case R.id.imageButton2:
                //do a google rating....
                break;

        }
    }


};

共享对话框下面有一条波浪形的红线,表示无法解析该方法。我该怎么办?我希望sharedialog在单击按钮后显示并传递图像和文本,但它不允许我在onclicklistener方法中使用任何方法。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

     ShareLinkContent content = new ShareLinkContent.Builder().setContentTitle(strTitle).setContentDescription(strDesc).setImageUrl(uri).
                                .setContentUrl(android.net.Uri.parse(extractLinks(strDesc))).build();
                        ShareDialog shareDialog = new ShareDialog(ViewFacebook.this);
                        shareDialog.show(content, ShareDialog.Mode.AUTOMATIC);
                        shareDialog.registerCallback(callbackManager, mShareDialog);

 private FacebookCallback<Sharer.Result> mShareDialog = new FacebookCallback<Sharer.Result>() {

        @Override
        public void onSuccess(Sharer.Result result) {
            Toast.makeText(ViewFacebook.this, "Publish Success!"), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onCancel() {
            Toast.makeText(ViewFacebook.this,
                    "Publish cancelled",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(FacebookException error) {
            Toast.makeText(ViewFacebook.this, error.getMessage(), Toast.LENGTH_LONG).show();
        }
    };