Search code examples
androidbitmaparraysksoap2

Read byte array in Android using k-soap2


I'm using this code in .net web service to send image as byte array:

public byte[] getImage()
{
    byte[] img;
    ....
    return img;
}

How can read this byte array using ksoap2 and convert it to bitmap?

Can you explain that in simple code.

Update: This code I'm using in android to read data from web service:

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

Solution

  • Considering that you have consumed the web service successfully, and retrieved the bytes, all you need to do is to decode the data and retrieve the Bitmap from it:

    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
    }
    

    If you have issues with the decoding part, please refer to this answer and its links too.