【问题标题】:how to convert base64 string image to blob in android?如何在android中将base64字符串图像转换为blob?
【发布时间】:2014-10-18 06:34:36
【问题描述】:

我有一个 Base64 字符串图像。我想将其转换为 blob 类型并将其保存在数据库中。 我有

String base64Image="iVBORw0KGgoAAAANSUhEUgAAAGAAAACgCAYAAADzcGmMAAAACSV...";

这是我试过的..

 byte[] byteImage=org.apache.commons.codec.binary.Base64.decodeBase64(befImage.getBytes());

json.put("during_unloading_photo", byteImage); 

json 是我的 JSONObject 并且 during_unloading_photo 是 blob 类型的列名。

【问题讨论】:

  • 请不要更改问题,使其含义完全不同,现有答案不再适用。如果您还有其他问题,请打开一个新问题。

标签: android


【解决方案1】:

要将blob保存到DataBase,您必须将base64 satring图像转换为Byte[],然后您可以将byte[]保存到db。 用于将base64 string转换为位图

 public static Bitmap decodeBase64Profile(String input) {
    Bitmap bitmap = null;
    if (input != null) {
        byte[] decodedByte = Base64.decode(input, 0);
        bitmap = BitmapFactory
                .decodeByteArray(decodedByte, 0, decodedByte.length);
    }
    return bitmap;
}

现在您可以将这个Bitmap 转换为Byte[] Like:

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;
}

最后你可以将这个 Byte[] 保存到 Db。确保你的列名有 typeblob

享受你的代码:)-

【讨论】:

    【解决方案2】:

    getBytes 为您提供字符串的字节,它不进行 base64 解码。您将需要使用 Base64 解码器进行转换。稍作搜索就会显示几个可供您使用的选项。

    【讨论】:

    • 我尝试使用 byte[] byteImage=Base64.decode(base64Image, Base64.DEFAULT);但它不起作用..
    • 你好像用过android.util.Base64,对吧?究竟是什么不工作?
    • 我使用了 android.util.Base64。我正在尝试从存储为 Base64 字符串的移动数据库中获取图像并将其转换为 blob 类型,以便我可以将其保存在主数据库中。
    • 我使用了 byte[] byteImg=org.apache.commons.codec.binary.Base64.decodeBase64(befImage.getBytes()); dato.put("beforeUnloadingPhoto",byteImg );
    • 即使在使用 org.apache.commons.codec.binary.Base64.decodeBase64 之后,我也无法将 byteImg 同步到数据库..
    猜你喜欢
    • 1970-01-01
    • 2015-04-22
    • 2019-10-03
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2020-03-16
    • 1970-01-01
    相关资源
    最近更新 更多