简体   繁体   English

如何使用Android中的Facebook SDK在Facebook中上传图片

[英]How can I upload an image in facebook using facebook SDK in android

I am trying to uplaod an image in my wall, but it only updates an post. 我正在尝试对墙上的图片进行修饰,但这只会更新帖子。 If there is any other way to upload an image, pls help me. 如果还有其他上传图片的方法,请帮帮我。 I logged in with the facebook log_in button widget. 我使用facebook log_in按钮小部件登录。 I didn't create any facebook object. 我没有创建任何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();
    }
}

See the Feed dialog documentation here: 请在此处查看Feed对话框文档:

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

The "source" parameter only accepts URLs. “源”参数仅接受URL。 If you want to upload an image, you should get the publish_actions permissions from the user, and use Request.newUploadPhotoRequest method. 如果要上传图像,则应从用户那里获得publish_actions权限,并使用Request.newUploadPhotoRequest方法。

You may want to try adding a parameter with picture url. 您可能要尝试添加带有图片网址的参数。
Example

    postParams.putString("picture", PICTURE_URL_HERE );

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM