加载方法
String base64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAABXCAYAAACTFMIVAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAA..." byte[] decodedString = Base64.decode(base64, Base64.DEFAULT); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); imageView.setImageBitmap(decodedByte);
这样的会报如下错误
java.lang.IllegalArgumentException: bad base-64 at android.util.Base64.decode(Base64.java:163) at android.util.Base64.decode(Base64.java:138) at android.util.Base64.decode(Base64.java:120)
这是因为前面的前缀
data:image/png;base64,
是不属于base64的编码里的一部分
完整的DataURL语法:DataURL= data:mediatype;base64,<Base64编码的数据>。
将DataURL前面的 data:image/png;base64, 删除
String base64 = "iVBORw0KGgoAAAANSUhEUgAAAFAAAABXCAYAAACTFMIVAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAA..." byte[] decodedString = Base64.decode(base64, Base64.DEFAULT); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); imageView.setImageBitmap(decodedByte);