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