0

How can I post an image with text on facebook? I know how to post text, links, but I want to post an image as well.

Here is what I have tried:

 byte[] data = null;
 Bitmap bi = BitmapFactory.decodeFile("R.drawable.plate1");
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
 data = baos.toByteArray();
 parameters.putByteArray("picture", data);

 int plate = getResources().getIdentifier("com.b2creativedesigns.eyetest:drawable/plate1", null, null);
 parameters.putString("picture", String.valueOf(plate)); 

Neither of them works. When I try to post, nothing happens. Nothing is posted. With the code below, it works.

Posting the image from a website is easy, but it will not work, when the website gets shutdown or the links modified.

  parameters.putString("picture", "https://lh3.ggpht.com/f79UCpnLisZxO2P2C43f55YLvFpNco_cTcC-t9Ck-Qmqe5jwKbfnUvCh5N6-Te-mOw=w124");

3rd example:

  byte[] data = null;
  Bitmap bi = BitmapFactory.decodeFile("R.drawable.plate1");
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  data = baos.toByteArray();
  parameters.putString("method", "photos.upload");
  parameters.putByteArray("picture", data);
erdomester
  • 11,789
  • 32
  • 132
  • 234

2 Answers2

3
Session.openActiveSession(activity, true,
                            new Session.StatusCallback() {
                                @Override
                                public void call(Session session,
                                        SessionState state,
                                        Exception exception) {
                                    // TODO Auto-generated method stub
                                    if (session.isOpened()) {
                                        fbSession = session;
    Bundle bundle = new Bundle();
    bundle.putString("caption", "Check this out, what do you think?");
    bundle.putString("name", name);
    bundle.putString("description", description);
    bundle.putString("picture", image);
    new WebDialog.FeedDialogBuilder(HomeActivity.this, session, bundle)
            .build().show();
                                    }
                                }

                            });

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    default:
        if (Session.getActiveSession() != null)
            Session.getActiveSession().onActivityResult(this, requestCode,
                    resultCode, data);
        break;
    }
}

Add this code in your Android Manifest File, put your facebook id on place of FB_AppID.

<activity android:name="com.facebook.LoginActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar"/><meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/FB_AppID"/>
Jaydip Meghapara
  • 2,687
  • 1
  • 15
  • 14
0

EDIT: Sorry, I shouldn't have gave advice on how to use the REST API since it's deprecated. This is how you upload a photo to your own wall using the Graph API.

Bundle params = new Bundle();
params.putByteArray("photo", data);
params.putString("caption", "Test photo upload");
    mAsyncRunner.request("me/photos", params, "POST",
    new PhotoUploadListener(), null);

Please try this and see if it works. This was taken straight out of our HackBook example in our documentation. For the compression of the image, you can use our Utility.scaleImage method inside our SDK which will do all the compression and scaling to match Facebook's standards.

==================================================================================

ORIGINAL MESSAGE:

photos.upload is using the REST API and is deprecated so I would recommend using the Graph API to upload photos but here's the quick and dirty solution using the REST API.

Once you have the byte array in a variable like data, do the following:

Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", data);
mFacebook.request(params);

Let me know if that helps.

Jesse Chen
  • 4,928
  • 1
  • 20
  • 20
  • updated my answer, please try my new suggestion and let me know if that works. – Jesse Chen Aug 17 '12 at 21:51
  • Is this going go create something like this? (http://www.johnhaydon.com/wp-content/uploads/2012/01/friends-can-comment.png) See, this is a posted text, with a link and an image. I want this, not just post an image. – erdomester Aug 19 '12 at 07:00
  • That is an open graph post and not simply posting an image. You should read the documentation to do that here https://developers.facebook.com/docs/opengraph/ – Jesse Chen Aug 20 '12 at 17:39