尝试在Android中自定义Facebook的共享对话框时遇到了一些问题。基本上我要做的是从ImageView组件获取可绘制图像,然后将其作为参数传递到我的共享对话框中。以下是我将图像设置为ImageView的代码:
ivEventDtl.setImageDrawable(EventDrawableImage.resizeEventDetailImage(
eventModel.getEventPic(), context));
这个是在onCreate()中完成的。 eventModel.getEventPic()是一个字符串。然后当点击我的facebook按钮时,我正在执行此操作:
@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() {
}
});
}
将drawable转换为字节数组的方法:
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;
}
如果我用URL硬编码图片,它工作正常。当我将其更改为convert drawable到byte array 1时,共享对话框中的所有参数都不会显示。有什么想法吗?
提前致谢。
修改 我修改了代码以获取图片字符串并调整大小超过200x200然后将其转换为位图,然后将其作为参数传递:
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();
}
答案 0 :(得分:0)
您无法直接在params中传递图像位图,您必须传递URI。 URI必须指向facebook应用程序可以访问的资源。您可以从位图中快速获取URI,如下所示:
String path = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "temp", "Test Image");
params.putString("picture", path);
使用它而不是直接将图片设置为位图或位图字节。
答案 1 :(得分:0)
Feed对话框不支持"图片"的二进制数据。参数。它必须是互联网可寻址的URL(即不是本地文件)。
有关详细信息,请参阅https://developers.facebook.com/docs/sharing/reference/feed-dialog/v2.2。