【发布时间】:2015-01-05 20:57:16
【问题描述】:
我正在尝试将图像 (BLOB) 的 64 位编码字符串转换为位图,以便可以在图像视图上显示它。 64 位解码的代码工作正常并返回 byte-stream ,但将返回的 byte-stream 转换为 Bitmap 会返回 null 。 请帮忙。
//This statement works
byte[] decodedString = Base64.decode(c.getString(TAG_PICTURE).toString().getBytes(),
Base64.DEFAULT);
//This statement returns null
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
完整代码如下:-
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
products = jsonObj.getJSONArray(TAG_PRODUCTS);
// looping through All Contacts
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
Double prod_price = Double.valueOf(c.getString(TAG_PRICE));
//The code below works as it returns the byte-array
byte[] decodedString = Base64.decode(c.getString(TAG_PICTURE), Base64.DEFAULT);
//The code below returns null even though the byte-stream is known
//from above
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
myProductsList.add(new PRODUCT(c.getString(TAG_PRODUCT_NAME),c.getString(TAG_STORE_NAME), c.getString(TAG_OWNER_NAME), c.getString(TAG_DESCRIPTION), prod_price,bitmap));
}
} catch (JSONException e) {
e.printStackTrace();
}
我很感激这个问题的解决方案,因为我花了很长时间搜索,现在被困在这个问题上。
提前致谢。
【问题讨论】:
标签: android imageview base64 bytearray blob