我想通过Web服务将图像发送到Android设备。这个Web服务是用VB.net编写的,Android设备正在调用它来获取数据。我可以正常获取数据,但问题出在Images上。我已将图像转换为字节数组,将其转换为字符串并通过Web服务(XML)传递给Android设备。我无法将此String字节数组解码回android端的图像。怎么做到这一点?有什么简单的解决我使用错误的做法吗?
答案 0 :(得分:3)
将字节数组转换为String有点棘手。由于图像数据可能包含空字节,因此String不能。您可能需要对字节数组进行编码,以使String有效。尝试使用base64编码。
请考虑以下代码示例:
System.Convert.ToBase64String(byteArray)
您还需要在android中解码base64编码的String。这也是一个例子:
import org.apache.commons.codec.binary.Base64;
String base64String = "... your base64 String ...";
byte[] decodedBytes = Base64.decodeBase64(base64String.getBytes());
decodedBytes
将包含您的原始数据。
答案 1 :(得分:0)
我们得到了解决方案
在网络服务方面我们使用了:
System.Convert.ToBase64String(byte_arr)
在Android端,我们使用了
byte[] decodedBytes = Base64.decode(imageByteArray[i].getBytes(),Base64.DEFAULT);
Bitmap bMap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
谢谢你的帮助。干杯!!!
答案 2 :(得分:0)
您可以使用org.castor.util.Base64Encoder将字节数组编码为String
byte [] doc = {37, 80, 68, 70, 45, 49, 46};
String image = new String(Base64Encoder.encode(doc));
在您的Web服务类中,使用此
byte[] imageData = Base64Decoder.decode(image);