如何在Android活动中从位图创建Blob?

5

我正在尝试创建新的MyImage实体,如如何使用Google App Engine上传和存储图像中所列出的。

现在我没有使用任何表单。我有一个从画廊获取Uri的Android应用程序:

m_galleryIntent = new Intent();
m_galleryIntent.setType("image/*");
m_galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

m_profileButton.setOnClickListener(new OnClickListener()
{
    public void onClick(View v) 
    {
        startActivityForResult(Intent.createChooser(m_galleryIntent, "Select Picture"),1);      
    }
});

我正在使用 Uri 创建 Bitmap。
如何从 Bitmap 在客户端中创建 Blob?
我需要添加哪些 JAR 文件到我的 Android 项目中?

这是使用 Blob 的正确方式吗?
我的主要目标是将从 Android 上传的图像保存在 GAE datastore 中,我是否正确地使用了这些工具或者有更好的方法?谢谢。


1
这实际上是与您的另一个问题相同的问题,如何从Android流式传输位图到Google App Engine Servlet?。请不要发布重复内容。 - Nick Johnson
2个回答

15

您需要将Bitmap转换为byte[],然后才能将其存储在数据库中的Blob中。

要将Bitmap转换为byte[],可以使用以下代码:

Bitmap yourBitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
yourBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] bArray = bos.toByteArray();

我希望这是你想要的。


1

您可以使用以下代码:

public static byte[] getBytesFromBitmap(Bitmap bitmap) {
    if (bitmap!=null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
        return stream.toByteArray();
    }
    return null;
}

用于将 bitmap 转换为 blob。

注意:

blob 是二进制大对象。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接