我在.net web服务中使用此代码将图像作为字节数组发送:
public byte[] getImage()
{
byte[] img;
....
return img;
}
如何使用ksoap2读取此字节数组并将其转换为位图?
你能用简单的代码解释一下。
更新: 这个代码我在android中用来从web服务中读取数据:
String SOAP_ACTION = WebServiceNameSpace + GetImage;
String NAMESPACE = WebServiceNameSpace;
String METHOD_NAME = GetImage;
String URL = WS_URL;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope Envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
Envelope.dotNet = true;
Envelope.setOutputSoapObject(request);
HttpTransportSE transport = new HttpTransportSE(URL);
transport.call(SOAP_ACTION, Envelope);
SoapPrimitive primetive = (SoapPrimitive) Envelope.getResponse();
return primetive.toString();
答案 0 :(得分:2)
考虑到您已成功使用Web服务并检索字节,您只需解码数据并从中检索Bitmap
:
final SoapPrimitive primitive = (SoapPrimitive) Envelope.getResponse();
final String imgData = primitive.toString();
if (imgData != "")
{
byte[] imgBytes = Base64.decode(imgData, Base64.DEFAULT);
final Bitmap bitmap = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);
// TODO: set this bitmap into an ImageView or handle it as you wish
}
如果解码部分有问题,请参阅this answer and its links。