1

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

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

decodeByteArray总是返回 null。

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

然后我尝试使用带有 Convert.Base64() 方法的 .NET 客户端解析这些数据。并且此图像已成功处理。然后我比较使用 android 客户端和 .NET 客户端创建的图像中的字节数组。不同之处在于字节的符号。.NET 使用无符号字节,但 Java 仅使用有符号字节。这是我的问题的原因吗?

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

4

1 回答 1

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;您应该将 commons-codec-1.3.jar 包含在 assets 文件夹中

版本不必是 1.3

感谢我的一位朋友的提示。

于 2011-02-17T11:26:59.200 回答