将byte []转换为Bitmap无效 - 空指针异常

时间:2016-09-20 08:26:52

标签: android base64 android-bitmap

当我执行bitmap.getHeight();我得到了一个nullPointerException。这就是我尝试获取位图的方法:

在我的json:" pic":" iVBORw0KGgoAAAANSUhEUgAAAgAAAAGACAIAAABUQk3 ......."

我从json检索以下内容:

byte[] decode = Base64.decode(jsonObj.getString("pic"), Base64.DEFAULT);
Log.i("size",decode.length+""); //65535
Bitmap pic = getImage(decode);

public static Bitmap getImage(byte[] image) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    return BitmapFactory.decodeByteArray(image, 0, image.length,options);
}

但我无法显示图像。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

错误必须在BitmapFactory.decodeByteArray内,并且很可能问题的根源是不支持的图像格式。检查是否传递了base64编码的jpeg,png,gif或bmp。

另外如果你得到65535作为decode的大小 - 这是非常可疑的。也许数据在数据库中被修剪(如果blob大小限制为65535)或其他地方。

相关问题