1

我正在尝试在我的墙上上传一张图片,但它只会更新一个帖子。如果有其他上传图片的方法,请帮助我。我使用 facebook log_in 按钮小部件登录。我没有创建任何 facebook 对象。

public void image_load(){
    Session session = Session.getActiveSession();

    if (session.isOpened())
    {       
        Bundle postParams = new Bundle();

        byte[] data = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Bitmap bi = BitmapFactory.decodeResource(getResources(),R.drawable.afzal);
        bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
        data = baos.toByteArray();

        //postParams.putByteArray("picture", data);
        postParams.putString("name", "Name here.");
        postParams.putString("caption", "Caption here.");
        postParams.putString("description", "Description here.");
        //postParams.putString("message", "This is message");
        postParams.putByteArray("source", data);

        //postParams.putString("method", "photos.upload");


        WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(this, Session.getActiveSession(), postParams))
                .setOnCompleteListener(new OnCompleteListener() {

                @Override
                public void onComplete(Bundle values, FacebookException error) {
                    if (error == null) {
                        // When the story is posted, echo the success
                        // and the post Id.
                        final String postId = values.getString("post_id");
                        if (postId != null) {
                            Toast.makeText(MainActivity.this,
                                "Posted story, id: "+postId,
                                Toast.LENGTH_SHORT).show();
                        } else {
                            // User clicked the Cancel button
                            Toast.makeText(MainActivity.this, 
                                "Publish cancelled", 
                                Toast.LENGTH_SHORT).show();
                        }
                    } else if (error instanceof FacebookOperationCanceledException) {
                        // User clicked the "x" button
                        Toast.makeText(MainActivity.this, 
                            "Publish cancelled", 
                            Toast.LENGTH_SHORT).show();
                    } else {
                        // Generic, ex: network error
                        Toast.makeText(MainActivity.this, 
                            "Error posting story", 
                            Toast.LENGTH_SHORT).show();
                    }
                }

            }).build();
        feedDialog.show();
    }
    else{
        Toast.makeText(MainActivity.this, "Please login first", Toast.LENGTH_SHORT).show();
    }
}
4

2 回答 2

1

请参阅此处的 Feed 对话框文档:

https://developers.facebook.com/docs/reference/dialogs/feed/

“source”参数只接受 URL。如果你想上传图片,你应该从用户那里获得 publish_actions 权限,并使用Request.newUploadPhotoRequest方法。

于 2013-10-02T22:23:21.440 回答
1

您可能想尝试添加带有图片 url 的参数。
例子

    postParams.putString("picture", PICTURE_URL_HERE );

希望这可以帮助。

于 2014-05-14T07:41:56.233 回答