关于Java:如何在Android中将Base64 Blob字符串转换为图像?

How to convert a Base64 blob string into image in android?

本问题已经有最佳答案,请猛点这里访问。

我从我的Web服务接收到Base64 Blob响应。我想在活动中创建一个图像并将其设置为Imageview。这是代码片段。
抱歉,请粘贴回复。

bs64img-Base64字符串响应
代码-

1
2
3
4
5
byte[] imageBytes=Base64.decode(bs64img, Base64.DEFAULT);
InputStream is = new ByteArrayInputStream(imageBytes);
Bitmap image=BitmapFactory.decodeStream(is);
ImageView i=(ImageView)findViewById(R.id.imageView1);
i.setImageBitmap(image);

请帮助我。


您可以尝试:

1
2
3
byte[] imgBytesData = android.util.Base64.decode(yourBase64String);
Bitmap bitmap = BitmapFactory.decodeByteArray(imgBytesData, 0, imgBytesData.length);
... then setting your ImageView or saving on FileSystem or ...


通常,嵌入式图像表示为

1
<img src="data:image/png;base64,iVBORw0KGg...">

即,文件的所有字节均已给出。因此,请尝试:

1
2
3
byte[] imageBytes=Base64.decode(bs64img, Base64.DEFAULT);
InputStream is = new ByteArrayInputStream(imageBytes);
BufferedImage img = ImageIO.read(is);