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



我正在尝试按照如何使用谷歌应用引擎上传和存储图像中列出的方式创建新的MyImage。

现在我没有使用任何表单。我有从图库中获取 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 来创建位图。
如何从位图在客户端中创建 Blob?
我必须将哪些罐子添加到我的安卓项目中?

这是使用 Blob 的正确方法吗?
我的主要目标是在 GAE 数据存储中保存从 android 上传的图像,我正确使用此工具还是更好的方法?那克斯。

必须将

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

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

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

我希望这是你想要的。

您可以使用以下代码:

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 是一个二进制大型对象。

最新更新