【问题标题】:How to convert base64 binary to bitmap in android?如何在android中将base64二进制转换为位图?
【发布时间】:2019-11-16 14:33:42
【问题描述】:

我有一个从 mongoDB 中的图像获得的 base64 二进制字符串。例如:

[255,216,255,224,0,16,74,70,73,70,0,1,1,1,0,96,0,96,0,0,255,219,0,67,0,8,6,6, 7,6,5,8,7,7,7,9,9,8,10,12,20,13,12,11,11,12,25,18,19,15,20,29,26, 31,30,29,26,28,28,32,36,46,39,32,34,44,35,28,28,40,55,41,44,48,49,52,52,52, 31,39,57,61,56,50,60,46,51,52,50,255,219,0,67,1,9,9,9,12,11,12,24,13,13,24,50, 33,28,33,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50, .................,253,246,255,0,190,141,20,80,2,249,142,57,222,223,153,160,200,231,146,237,159,173,20,80,1,230,63,247,219,254,250,52,155,223,251,237,255,0,125,26,40,160,4,103,102,108,177,44, 79,82,78,73,162,138,40,3,255,217]

在 Android 中,如何将此字符串转换为位图?此代码不起作用:

byte[] decodedString = {Base64 Binary Here}.getBytes();
Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

这是我的完整代码:

// JSON Parser
JsonParser jsonParser = new JsonParser();
JsonArray jsonArray = (JsonArray) jsonParser.parse(json_result);

String thirdParse = null;

for (int i = 0; i < jsonArray.size(); i++) {
    JsonObject object = (JsonObject) jsonArray.get(i);
    JsonObject firstParse = (JsonObject) object.get("img");
    JsonObject secondParse = (JsonObject) firstParse.get("data");
    thirdParse = secondParse.get("data").toString();
}

byte[] decodedString = thirdParse.getBytes();
Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

imageView.setImageBitmap(bmp);

这是我从 mongoDB 得到的 JSON:

[{"img":{"data":{"type":"Buffer","data":[(base64 binary data)]}}}]

【问题讨论】:

  • 请问mongodb给你的是字节数组还是字符串JSON?
  • 它返回一个 JSON 字符串。
  • 你能添加你在问题中收到的 JSON 吗?
  • 我添加了您请求的 JSON。

标签: android image binary base64


【解决方案1】:

好吧,经过一番深入研究,它不起作用的原因是因为这里

byte[] decodedString = thirdParse.getBytes();

您没有从字符串中获取字节数组,您实际上是在将thirdParse 转换为字节数组,所以如果您得到例如thirdparse = [1,2,3],当您使用getBytes()它将这个数组转换为字节,你想要的是直接取值本身而不转换任何东西,所以这就是我所做的

首先我创建了一个方法,您可以将它提供给thirdParse

public byte[] convertToByteArray(String stringContainingByteArray){
 String[] l =    stringContainingByteArray.replace("[","").replace("]","").split(",");
 byte [] byteArray = new byte[l.length];
 for (int i=0;i<l.length;i++){
     byteArray[i]=Byte.parseByte(l[i]);
 }
    return byteArray;
}

您只需将其发送第三个解析字符串并检索一个字节数组,您将按如下方式将其传递给 BitmapFactory

    byte[] byteArray = convertToByteArray(stringContainingByteArray)
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    imageView.setImageBitmap(bmp)

为简单起见,没有方法的整个代码如下

// JSON Parser
    JsonParser jsonParser = new JsonParser();
    JsonArray jsonArray = (JsonArray) jsonParser.parse(json_result);

    String thirdParse = null;

    for (int i = 0; i < jsonArray.size(); i++) {
        JsonObject object = (JsonObject) jsonArray.get(i);
        JsonObject firstParse = (JsonObject) object.get("img");
        JsonObject secondParse = (JsonObject) firstParse.get("data");
        thirdParse = secondParse.get("data").toString();
    }
    String[] l =    thirdParse.replace("[","").replace("]","").split(",");
    byte [] decodedString = new byte[l.length];
    for (int i=0;i<l.length;i++){
        decodedString[i]=Byte.parseByte(l[i]);
    }
    Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

    imageView.setImageBitmap(bmp);

【讨论】:

    【解决方案2】:

    我只是在mongoDB连接中将我的图像保存类型从Buffer更改为String,然后将编码更改为base64 String。

    【讨论】:

      猜你喜欢
      • 2016-10-13
      • 2011-08-05
      • 2017-08-21
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 2012-09-01
      • 2018-01-29
      • 2017-08-08
      相关资源
      最近更新 更多