繁体   English   中英

Java将drawable转换为bytearray

[英]Java convert drawable to bytearray

尝试在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对图片进行硬编码,则效果很好。 当我将其更改为可转换可绘制为字节数组一时,共享对话框中的所有参数均未显示。 有任何想法吗?

提前致谢。

编辑我修改了代码以接受图片字符串,并将其大小调整为大于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();
}

您不能直接在参数中传递图像位图,而必须传递URI。 URI必须指向Facebook应用程序可以访问的资源。 您可以像这样从位图快速获取URI:

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

使用它代替直接将图片设置为位图或位图字节。

供稿对话框不支持“图片”参数的二进制数据。 它必须是可通过互联网访问的URL(即不是本地文件)。

有关更多信息,请参见https://developers.facebook.com/docs/sharing/reference/feed-dialog/v2.2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM