简体   繁体   中英

Android Post Image to Facebook

I have this code running in my app and it is trying to post text and an image to a users wall. At the moment it is only posting the text. I think I have missed something simple, but would appreciate another pair of eyes check everything or another sample.

Any help is greatly appreciated.

            Bundle bundle = new Bundle();
            bundle.putString("message", "test update"); //'message' tells facebook that you're updating your status
            bundle.putString(Facebook.TOKEN,accessToken);
            bundle.putString("attachment", "{\"name\":\"My Test Image\","
+"\"href\":\""+"http://www.google.com"+"\","
+"\"media\":[{\"type\":\"image\",\"src\":\""+"http://www.google.com/logos/mucha10-hp.jpg"+"\",\"href\":\""+"http://www.google.com"+"\"}]"
+"}");
                    +"}");
            //tells facebook that you're performing this action on the authenticated users wall, thus 
//          it becomes an update. POST tells that the method being used is POST
            String response = facebook.request("me/feed",bundle,"POST");

Hope this will be work for you

Create class for facebook varible utility

import android.app.Application;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;

public class Utility extends Application{
    public static Facebook mFacebook;
    public static AsyncFacebookRunner mAsyncRunner;
    public static String userUID;
    public static final String ICON_URL = "http://i.imgur.com/6G1b7.png";

}

Now mathod used to post image to facebook wall

public void postOnFacebookPicture(final Bitmap bitmap) {

        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);

        if (access_token != null) {
            Utility.mFacebook.setAccessToken(access_token);

        }
        if (expires != 0) {
            Utility.mFacebook.setAccessExpires(expires);
        }

        if (!Utility.mFacebook.isSessionValid()) {
            showErrorDialog(
                    "Facebook Account is not configure,Setting Facebook Account?",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            try {
                                 // Move to setting the facebook account
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    }, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();

                        }
                    });
        } else {
            new Thread() {
                @Override
                public void run() {
                    int what = 0;

                    try {

                        String accessToken = mPrefs.getString("access_token",
                                null);

                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        bitmap.compress(CompressFormat.PNG, 0, bos);
                        byte[] pictureData = bos.toByteArray();

                        Bundle bundle = new Bundle();
                        bundle.putByteArray("facebookPictureData", pictureData);
                        bundle.putString(Facebook.TOKEN, accessToken);

                        Utility.mFacebook.request("me/photos", bundle, "POST");

                    } catch (Exception e) {
                        what = 1;
                    }

                    mHandler.sendMessage(mHandler.obtainMessage(what));
                }
            }.start();
        }

    }

For Posting Text and Image in Facebook wall, check this link :

You can insert Image using Media attachment.

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