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

问题描述:

我正在从我的Web服务接收Base64 blob响应。我想在我的活动中创建一个图像并将其设置为Imageview。这是代码片段。 道歉粘贴响应。如何在Android中将Base64 Blob字符串转换为图像?

bs64img - Base64编码字符串响应 码 -

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); 

请帮我在这。

+2

您不必在此发布响应,而应该发布您试图将其转换为“Image”的代码片段。 – SudoRahul 2013-04-09 10:13:04

+0

检查此:http://*.com/a/3801881/1168654 – 2013-04-09 10:14:49

+0

@ jaa-c - >我使用该链接上的代码。得到这个错误--- SkImageDecoder :: Factory返回null – 2013-04-09 10:25:41

你可以尝试:

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

得到这个错误--- SkImageDecoder :: Factory返回null - – 2013-04-09 10:29:54

一般来说,嵌入式图像给出

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

也就是说,该文件的所有字节中给出。所以请尝试:

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