erdomester
erdomester

Reputation: 11839

Post image with text on facebook from android

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);

Upvotes: 0

Views: 2092

Answers (2)

Jaydip Meghapara
Jaydip Meghapara

Reputation: 2843

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"/>

Upvotes: 3

Jesse Chen
Jesse Chen

Reputation: 4928

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.

Upvotes: 0

Related Questions