简体   繁体   中英

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. I didn't create any facebook object.

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:

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

The "source" parameter only accepts URLs. If you want to upload an image, you should get the publish_actions permissions from the user, and use Request.newUploadPhotoRequest method.

You may want to try adding a parameter with picture url.
Example

    postParams.putString("picture", PICTURE_URL_HERE );

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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