【问题标题】:How to Create a Blob from Bitmap in Android Activity?如何在 Android Activity 中从位图创建 Blob?
【发布时间】:2012-05-24 00:04:25
【问题描述】:

我正在尝试完全按照How to upload and store an image with google app engine 中列出的方式创建新的 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?
我必须将哪些 jars 添加到我的 android 项目中?

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

【问题讨论】:

标签: java android google-app-engine


【解决方案1】:

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

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

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

我希望这是你想要的。

【讨论】:

    【解决方案2】:

    您可以使用此代码:

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

    【讨论】:

      猜你喜欢
      • 2013-11-13
      • 1970-01-01
      • 2011-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      相关资源
      最近更新 更多