使用自定义消息将图像发布到Facebook

时间:2013-12-02 13:56:28

标签: android

我正在使用下一个代码将图片发布到用户的Facebook墙上:

private void postImageToWall(Session session) {

    // Bitmap image = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "demo.jpg");
    Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.frame_with_woman);
    Bundle parameters = new Bundle();
    parameters.putParcelable("source", image);
    parameters.putString("message", "my message for the page");
    Request request = new Request(Session.getActiveSession(), "me/photos", parameters, HttpMethod.POST, new Request.Callback() {
        @Override
        public void onCompleted(Response response) {
            LogService.log(TAG, "Successfully posted");
        }
    });
    request.executeAsync();

}

如何制作这样一个“默认”Facebook弹出对话框(你知道那些,例如 this one )从而让用户输入图像的标题,而不是设置硬编码的标题,就像我在上面的代码中一样。

parameters.putString("message", "my message for the page");

2 个答案:

答案 0 :(得分:1)

尝试使用以下方法发布消息

void PostFb()
      {
        Bundle parameters = new Bundle();
        parameters.putString("name", getResources().getString(R.string.app_name));
        parameters.putString("link", "http://www.youtube.com");//Put Image URL Here


        facebook.dialog(this, "stream.publish", parameters,
                new DialogListener() {

                    public void onFacebookError(FacebookError e) {
                        e.printStackTrace();
                    }

                    public void onError(DialogError e) {
                        // TODO Auto-generated method stub
                        e.printStackTrace();

                    }

                    public void onComplete(Bundle values) {

                    }

                    public void onCancel() {

                    }
                });
    }

让我知道它是否有效。因为它会显示您在此行parameters.putString("link", "http://www.youtube.com");

输入的任何网址的预览

在此之前声明以下参数

声明上面的类名私人Facebook facebook;

&安培;在onCreate方法

进行这样的初始化
facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);

答案 1 :(得分:0)

将此标记用于描述发布,

postParams.putString("caption", mes);

这是我在Facebook墙上与文字共享图像的完整代码,

private void facebookImagePosting(String imageUrl, String mes) {
    Bundle postParams = new Bundle();
    postParams.putString("access_token", accessToken);

    byte[] data = null;

    URL aURL;
    BufferedInputStream bis = null;
    try {
        aURL = new URL(imageUrl);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        bis = new BufferedInputStream(is, 8192);

    } catch (Exception e) {

        e.printStackTrace();
    }

    Bitmap bi = BitmapFactory.decodeStream(bis);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    data = baos.toByteArray();
    postParams.putByteArray("picture", data);
    postParams.putString("caption", mes);
    Request.Callback callback = new Request.Callback() {
        public void onCompleted(Response response) {
            FacebookRequestError error = response.getError();
            if (error != null) {
                Log.e("Facebook Error Message", error.getErrorMessage());
            } else {
            Log.e("Success Message", "Decision posted successfully!");

            }
        }
    };
    Request request = new Request(Session.getActiveSession(), "me/photos",
            postParams, HttpMethod.POST, callback);
    RequestAsyncTask task = new RequestAsyncTask(request);
    task.execute();
}