0

I'd like to post an image to facebook by using their graph api. But my image is in drawable folder. So far:

public static void postToFacebook() {

    Bundle params = new Bundle();

    // shared with this
    params.putString("caption", activityContext.getString(R.string.app_name));
    // description of the program
    params.putString("description", "bla bla bla bla bla bla"); 
    // header of the description
    params.putString("name", "train");
    // callback url
    params.putString("link","http://www.google.com.tr");
    // put a personal message
    params.putString("message", "my message");
    // to put a photo
    params.putByteArray("picture", getImageAsData());

    try {
        String response = FacebookUtility.mFacebook.request("/me/feed",
                params, "POST");
        if (response == null || response.equals("")
                || response.equals("false"))
            Toast.makeText(activityContext, "Error.",
                    Toast.LENGTH_SHORT).show();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public static byte[] getImageAsData() {

Drawable d = activityContext.getResources().getDrawable(R.drawable.icon);
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] bitmapdata = stream.toByteArray();
    return bitmapdata;
}

But this is not working... It's not posting my icon, but it gets google icon from site. I try to comment out link part, but this time it didn't send anything but message.

I also try to get bitmap as:

Bitmap bitmap = BitmapFactory.decodeResource(activityContext.getResources(), R.drawable.icon);

But this also didn't work out.

I've seen:

Bitmap bi = BitmapFactory.decodeFile(pathonsdcard);

But i couldn't use this method to get bitmap, because i have no pathonsdcard?

Any suggestion? So how can i post image which is located in res/drawable folder ? Thanks in advance.

yahya
  • 4,810
  • 3
  • 41
  • 58

3 Answers3

1

This is the correct way to upload a photo from a drawable. Note that the only parameters you need is "photo" and "caption".

Bundle params = new Bundle();
params.putByteArray("photo", getImageAsData());
params.putString("caption", "Test caption lololol");
mAsyncRunner.request("me/photos", params, "POST", new PhotoUploadListener(), null);
Jesse Chen
  • 4,928
  • 1
  • 20
  • 20
  • But i don't want to post only photo, i need also some info about my application and user's comment too. So i can't use "me/photos"... – yahya Sep 07 '12 at 06:24
  • then you need to POST to me/feed but with a "picture" parameter that supplies an URL instead of a byte array. You can then customize the post with the "caption", "description", "name" and "message" parameters. – Jesse Chen Sep 07 '12 at 18:25
  • It's already like that in my question... But the problem is i want to send picture from drwable folder, i do not have any url for this picture... – yahya Sep 10 '12 at 06:22
0
Bundle b=new Bundle();
b.putString("Picture","// Byte array value of ur image");

facebook.request("username/photos",b,"POST");
Aditya Nikhade
  • 1,373
  • 10
  • 15
  • params.putString("link","http://www.google.com.tr"); try removing this and run... – Aditya Nikhade Sep 06 '12 at 11:36
  • what is actually happening??? the post u are posting is appearing or not??? and where do u want that icon of urs to show up!?? – Aditya Nikhade Sep 06 '12 at 11:44
  • if i post with url ( params.putString("picture", "http://www.facebookmobileweb.com/hackbook/img/facebook_icon_large.png"); ), it posts like this: http://www.2shared.com/photo/gYOC-gfV/Untitled.html I want that image to be from my drawable folder... – yahya Sep 06 '12 at 11:51
0

Instead of using BitmapDrawable you can directly get the image from drawable to bitmap then convert it to bytearray. Some times bitmapdrawable causes unexpected problem.

Bitmap bitmap = BitmapFactory.decodeResource(ctx.getResources(),
R.drawable.image);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();

now use this to post --

parameters.putByteArray("picture", bitMapData);
mAsyncFbRunner.request("me/feed", params, "POST", new WallPostListener());
Chinmoy Debnath
  • 2,814
  • 16
  • 20