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

时间:2012-05-16 12:16:35

标签: java android google-app-engine

我正在尝试创建How to upload and store an image with google app engine中列出的新MyImage。

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

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在我的客户端创建Blob?
什么罐我必须添加到我的android项目?

这是使用Blob的正确方法吗? 我的主要目标是保存从GAE数据存储区中的机器人升级的图像,Am正确使用此工具还是更好的方法? Thatks。

2 个答案:

答案 0 :(得分:13)

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

要将Bitmap转换为byte[],您可以使用:

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

我希望它是你想要的。

答案 1 :(得分: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是一个二进制大对象。