0

I was having some problem when trying to customize the share dialog of Facebook in Android. Basically what I am trying to do is get the drawable image from ImageView component then pass it as params into my share dialog. Here is the codes where I set image to my ImageView:

ivEventDtl.setImageDrawable(EventDrawableImage.resizeEventDetailImage(
            eventModel.getEventPic(), context));

This one was done in onCreate(). the eventModel.getEventPic() was a string. Then when my facebook button on click, I am executing this:

@SuppressWarnings("deprecation")
public void postToWall() {
    Bundle params = new Bundle();  
    params.putString("name", txtEventDtlName.getText().toString());
    params.putString("caption", "Date: " + txtEventDtlDate.getText().toString());
    params.putString("link", "");
    params.putString("description", txtEventDtlDesc.getText().toString());
    //params.putString("picture", "http://twitpic.com/show/thumb/6hqd44");
    try {
        params.putByteArray("picture", EventDrawableImage.extractBytes(ivEventDtl.getDrawable()));
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    facebook.dialog(getActivity(), "feed", params, new DialogListener() {

        public void onFacebookError(FacebookError e) {
        }

        public void onError(DialogError e) {
        }

        public void onComplete(Bundle values) {
        }

        public void onCancel() {
        }
    });

}

And the method to convert drawable to byte array:

public static byte[] extractBytes(Drawable image) throws IOException {
    Bitmap bitmap = ((BitmapDrawable) image).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] bitmapdata = stream.toByteArray();
    return bitmapdata;
}

It was working fine if I hardcode the picture with the URL. When I changed it to the convert drawable to byte array one, all the params in share dialog does not show. Any ideas?

Thanks in advance.

EDIT I modified the codes to take in the picture string and resize to larger than 200x200 then convert it into bitmap before passing it as params:

Drawable image = EventDrawableImage.resizeShareDialogueImage(eventPicPath, context);
Bitmap bitmap = EventDrawableImage.drawableToBitmap(image);

public static Drawable resizeShareDialogueImage(String eventPic,
        Context context) {
    String uri = "@drawable/" + eventPic;
    int imageResource = context.getResources().getIdentifier(uri, null,
            context.getPackageName());
    Drawable res = context.getResources().getDrawable(imageResource);
    Bitmap bitmap = ((BitmapDrawable) res).getBitmap();
    Drawable d = new BitmapDrawable(context.getResources(),
            Bitmap.createScaledBitmap(bitmap, 250, 250, true));
    return d;
}

public static Bitmap drawableToBitmap (Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable)drawable).getBitmap();
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap); 
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

public void postToWall() {
    Drawable image = EventDrawableImage.resizeShareDialogueImage(eventPicPath, context);

    Bitmap bitmap = EventDrawableImage.drawableToBitmap(image);

    final Bundle params = new Bundle();  
    params.putString("name", txtEventDtlName.getText().toString());
    params.putString("caption", "Date: " + txtEventDtlDate.getText().toString());
    params.putString("link", "");
    params.putString("description", txtEventDtlDesc.getText().toString());
    params.putParcelable("picture", bitmap);
    //params.putString("picture", "http://twitpic.com/show/thumb/6hqd44");
    Request request = new Request(Session.getActiveSession(), "me/photos", params, HttpMethod.POST, new Request.Callback()
    {
        public void onCompleted(Response response)
        {
            facebook.dialog(getActivity(), "feed", params, new DialogListener() {

                public void onFacebookError(FacebookError e) {
                }

                public void onError(DialogError e) {
                }

                public void onComplete(Bundle values) {
                }

                public void onCancel() {
                }
            });
        }
    });

    RequestAsyncTask task = new RequestAsyncTask(request);
    task.execute();
}
  • what you tried before that was working?? – Jordi Castilla Dec 23 '14 at 11:41
  • If I used the commented out that URL for picture, it was working. I not sure if it's the picture in byte array causing the share dialogue does not take in parameters, or my convert method got problem. There is no error message though –  Dec 23 '14 at 11:43

2 Answers2

0

You cant pass an image bitmap directly in the params, you must pass a URI. The URI must point a resource the facebook app can access. You can quickly get a URI from a bitmap like this:

String path = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "temp", "Test Image");
params.putString("picture", path);

Use that instead of setting picture directly to bitmap or bitmap bytes.

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • Nope, does not work. If I put back the byte array image, all my other params are missing from share dialogue as well –  Dec 23 '14 at 11:49
  • Well a quick google search shows you cannot pass image bytes to this function. You must pass a URI. You need to save bytes to file and pass URI to that file. Here is all the detail http://stackoverflow.com/questions/19145375/unable-to-post-an-image-from-drawable-to-facebook – Greg Ennis Dec 23 '14 at 11:53
  • But what should I do with that? I am confused that he didnt do anything with the bitmap :( –  Dec 23 '14 at 11:58
  • Can you help me take a look at the edited portion? I changed it but still, the image is breaking all my other params –  Dec 23 '14 at 12:13
  • You cant put a bitmap in the params. You have to save the bitmap to a file, and in the params, put the file path. – Greg Ennis Dec 23 '14 at 12:41
  • No you are still sticking the bitmap into the params. You can't do that. – Greg Ennis Dec 23 '14 at 12:46
  • But I thought that's what he does from the link you provided –  Dec 23 '14 at 12:47
  • Sorry that link i posted is using the uploadphotorequest which is fir uploading photos. It is different. I will uodate my answer.... One sec – Greg Ennis Dec 23 '14 at 12:50
  • Okay sure thanks a lot. I guess I will just grab some images online and use it as URI :) Thanks for the helps !! –  Dec 23 '14 at 13:00
  • You cant use the code I posted to post your drawable? – Greg Ennis Dec 23 '14 at 13:02
0

The feed dialog does not support binary data for the "picture" parameter. It must be an internet addressable URL (i.e. not a local file).

See https://developers.facebook.com/docs/sharing/reference/feed-dialog/v2.2 for more info.

Ming Li
  • 15,672
  • 3
  • 37
  • 35
  • I see. Thanks a lot! I guess I have to use internet addressable URL then :( –  Dec 24 '14 at 03:10