Wednesday, June 27, 2012

                  Post Image On Facebook Wall From android Application

This is the article to explain how to post image and text on the wall from android application.

To do this we have to follow the following steps.

CONFIGURE ANDROID FACEBOOK SDK

1.Download the facebook android sdk from " https://github.com/facebook/facebook-android-sdk/ "

 2.Open eclipse and create new project.
 3.After creating new project create new package "com.facebook.android"
 4.Now go to the facebook android sdk (as downloaded in point 1.) and extract the zip file .Now go to the folder com > facebook > android, and copy all the file in your eclipse projects package "com.facebook.android".Also copy all the resource folder data if you got any error.

Now you have configured the facebook android sdk in your android project.

ADD CODE IN YOUR ACTIVITY TO POST IMAGE

1.Open your activity and open xml file .In xml file add button from which you can post image .

2.Now you need the following data to post image on facebook wall.

Path of image you want to post.
Byte array of the image which you want to post.

First get the path of the image and add the following method in your activity.

public static byte[] getBytes() {

        byte[] bytes = imageLoader.getBytesOfImage(picUrl);
        return bytes;
    }


This method return the bytes of image .

Also add the onactivityresult method

 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent      data) {
        super.onActivityResult(requestCode, resultCode, data);

     
       facebook.authorizeCallback(requestCode, resultCode, data);

    }


Now create the following variable

Facebook facebook = Utility.getFacebook();
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
Handler  mHandler = new Handler();

Now onclick of the button add the following method.

public void postImageOnFacebookWall(final String wallMessage) {

        new AsyncTask<Void, Void, Boolean>() {

            protected void onPreExecute() {

                          };

            @Override
            protected Boolean doInBackground(Void... params) {

                try {

                    Bundle bundle = new Bundle();
                    try {

                        bundle.putByteArray("photo", getBytes());
                        bundle.putString("caption", wallMessage);

                       mAsyncRunner.request("me/photos", bundle,
                                "POST", new PhotoUploadListener(), null);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    return true;
                } catch (Exception e) {
                              Log.d("TAG", " Exception.." + e);
                    return false;
                }

            }

            @Override
            protected void onPostExecute(Boolean result) {
                super.onPostExecute(result);
                if (!result) {

                }

            }
        }.execute();
    }


Now add following classs in this activity

public class PhotoUploadListener extends BaseRequestListener {

        public void onComplete(final String response, final Object state) {

            mHandler.post(new Runnable() {
                @Override
                public void run() {

                    progressDialog = com.mobclub.utility.Utility
                            .stopProgressDialog(progressDialog);

                    ShowAlertAfterResult(ctx,
                            "Picture post on Facebook Wall successfully !");
                }
            });
        }

        public void onFacebookError(FacebookError error) {

            progressDialog = com.mobclub.utility.Utility
                    .stopProgressDialog(progressDialog);

            Toast.makeText(
                    getApplicationContext(),
                    "Facebook Error: "
                            + "Some error occurs from facebook.Please try again later.",
                    Toast.LENGTH_LONG).show();
        }
    }

    public class UserRequestListener extends BaseRequestListener {

        @Override
        public void onComplete(final String response, final Object state) {

            try {

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                    }
                });

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }


Remember to add the internet permission in your manifest file.

Now enjoy the code and post the image on facebook wall using android.