简体   繁体   中英

Send byte array from ms sql database to android through webservice

I have a C# web service that gets data from a MS SQL database and sends it to an Android device as a XML string.

I then parse the XML in Android and store the data in an Android database

This works fine for all string and integer data but I can't get a byte array (which is used to store an image) to work in same way

My C# code to add the byte array to XML looks like this:

XmlElement ImageStored = (XmlElement)StockItem.AppendChild(doc.CreateElement("ImageStored"));

ImageStored.InnerText = Convert.ToBase64String(stockItem.ImageStored);

And in Android i have tried this:

NodeList nImageStored = doc.getElementsByTagName("ImageStored");

for(int i = 0; i < nImageStored.getLength(); i++)
{
   byte[] pImageStored =  nImageStored.item(i).getFirstChild().getNodeValue().trim().getBytes();
   //save byte[] in database
}

This is not giving me any errors and does save something in the database, but when it comes to displaying the image nothing appears.

Any help on how to get this working would be great. This is the first time I have tried working with C# and Android together so forgive me if I am being stupid.

Thanks!

I'm not much of a Java guy but it looks like you should be using decode() instead of getBytes(). In other words you are just storing the Base64 encoded string in your database, instead of decoding it first.

public static byte[] decode (String str, int flags)

Since: API Level 8 Decode the Base64-encoded data in input and return the data in a new byte array. The padding '=' characters at the end are considered optional, but if any are present, there must be the correct number of them.

http://developer.android.com/reference/android/util/Base64.html

I have done almost the same where I used the thirdparty library ksoap2. Ok so here's an example, first I created a wrapper class for my object which needed the image:

public class ImageClass {
    private byte[] AttractionImage;
    private String AttractionImageBase64;
        // TODO getter and setters
}

First you need to decode the base64 string you get from the .NET webservice, for that I also used the ksoad library:

ImageClass imageClass = new ImageClass();
imageClass.setAttractionImage(Base64.decode("your base62 string"));

Let me know if you need more info :)

you can find ksoap here: http://code.google.com/p/ksoap2-android/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM