Android Upload Image to Facebook

This code works for me in my application, and it tries to publish the text and image on the user's wall. At the moment this is only the publication of the text. I think I missed something simple, but I would like another pair of eyes to check all 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");
0
source share
2 answers

I hope this will be your job.

Create a utility class for facebook varible.

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 is used to post images on 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();
        }

    }
+2
source

To post text and images to your Facebook wall, check out this link :

Media.

0

All Articles