Search code examples
androidbinaryinputstreambitmapfactory

How to convert Binary String into image in Android?


I am using .net web services ksoap and I am getting Binary string in response. I want to convert this string into image and want to display this image in ImageView. I am getting images/doc/pdf's from server.

My code:

SoapObject DocumentRequest = new SoapObject(NAMESPACE, GET_DOCUMENT_METHOD);

        DocumentRequest.addProperty("DocumentID", ID);
        Log.i("DocumentID", ID);

        SoapSerializationEnvelope Envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        Envelope.dotNet = true;
        Envelope.setOutputSoapObject(DocumentRequest);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;

        androidHttpTransport.call(SOAP_ACTION_GETDOCUMENT, Envelope);

        SoapPrimitive DocumentResponse = (SoapPrimitive)Envelope.getResponse();
        Log.i("DocumentResponse", DocumentResponse.toString());

What should I use to display any type of file image/doc/pdf. How to convert this binary string into correct format and how to download it??? I am so confused.

//Is bitmap is good Idea?

this code is giving me response in binary in log. I want to convert this in Image/doc file/pdf file and want to download it in local sd card.

POST /**********Mobile/**********.asmx HTTP/1.1
Host: 192.168.1.5
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetDocument"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
xmlns:xsd="http://www.w3.org/2001/XMLSchema"    
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
 <GetDocument xmlns="http://tempuri.org/">
   <DocumentID>string</DocumentID>
 </GetDocument>
</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body>
   <GetDocumentResponse xmlns="http://tempuri.org/">
     <GetDocumentResult>string</GetDocumentResult>
   </GetDocumentResponse>
 </soap:Body>
</soap:Envelope>

Pleasesuggest thanks


Solution

  • Ideally, you could get the API give you back a link to the doc/image, and then later, you can download it.

    Having binary data in a response, not only makes your code complex, but also, you parsing.

    Update: Converting a binary string into a file, is as easy as reading the data, and writing it into a file.

    I suggested, not to use this approach in the first part of my response.