Skip to content Skip to sidebar Skip to footer

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, bu

Solution 1:

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;

publicclassUtilityextendsApplication{
    publicstatic Facebook mFacebook;
    publicstatic AsyncFacebookRunner mAsyncRunner;
    publicstatic String userUID;
    publicstaticfinalStringICON_URL="http://i.imgur.com/6G1b7.png";

}

Now mathod used to post image to facebook wall

publicvoidpostOnFacebookPicture(final Bitmap bitmap) {

        Stringaccess_token= mPrefs.getString("access_token", null);
        longexpires= 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?",
                    newDialogInterface.OnClickListener() {
                        publicvoidonClick(DialogInterface dialog, int id) {
                            try {
                                 // Move to setting the facebook account
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    }, newDialogInterface.OnClickListener() {
                        publicvoidonClick(DialogInterface dialog, int id) {
                            dialog.cancel();

                        }
                    });
        } else {
            newThread() {
                @Overridepublicvoidrun() {
                    intwhat=0;

                    try {

                        StringaccessToken= mPrefs.getString("access_token",
                                null);

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

                        Bundlebundle=newBundle();
                        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();
        }

    }

Solution 2:

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

You can insert Image using Media attachment.

Post a Comment for "Android Post Image To Facebook"