2011-02-03 68 views
1

我使用web服务来获取图像。服务响应包含base64Binary格式的图像。我尝试使用Base64.decode()(http://iharder.sourceforge.net/current/java/base64/)解码响应数据。见下面我的代码:Android:从base64binary格式获取图像

byte[] data = Base64.decode(responseString); 
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length); 
imageView.setImageBitmap(bmp); 

decodeByteArray总是返回null。

我尝试在.png文件中保存数据。我可以在我的电脑和Android文件管理器应用程序中打开这个文件。但是文件管理器的预览活动无法打开这个文件。

然后,我尝试使用.NET客户端与Convert.Base64()方法解析此数据。这张图片已经成功处理。然后我比较用Android客户端和.NET客户端创建的图像中的字节数组。差异是以字节为单位的。 .NET使用无符号字节,但Java仅使用带符号字节。这是我的问题的原因吗?

是否有人在解码base64Binary时遇到同样的问题?

+0

你确定`responseString`和'data`不为null吗? – user432209 2011-02-03 16:55:03

回答

2

这里是一个解决方案,对我来说是工作(知道其中的图像来自服务器通过Web服务的格式是使用Base64Binary)

decodedIcon[] = null; 
byte[] bb = (resposeString).getBytes("utf-8"); 
decodedIcon = Base64.decodeBase64(bb); 

Bitmap bitmap = BitmapFactory.decodeByteArray(decodedIcon, 0, 
decodedIcon.length); 

//then you get the image view and setImageBitmap(bitmap) 

PS:

Base64.decodeBase64来自库org.apache.commons.codec.binary.Base64; 你应该列入资产文件夹

公地编解码器1.3.jar版本不必须是1.3

感谢我的朋友为这个线索之一。