0

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!

tobias86
5,0291 gold badge23 silver badges31 bronze badges
asked Mar 20, 2012 at 11:47
1
  • Remember that the byte array is encoded in a base64 string from the .NET webservice and therefore have to decode it. I given a small overview how this can be done with ksoap below. Commented Mar 20, 2012 at 13:22

2 Answers 2

1

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

answered Mar 20, 2012 at 13:13
1

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/

answered Mar 20, 2012 at 13:17
5
  • thanks, i already am using ksoap to make requests to the c# service, i tried what you said using base64.decode(nImageStored.item(i).getFirstChild().getNodeValue().toString()); but this is just causing my application to crash, with no exception getting caught, any idea why this would be? Commented Mar 20, 2012 at 14:15
  • What does the value give you: nImageStored.item(i).getFirstChild().getNodeValue().toString() ? is it a string and is it a base64 string? Commented Mar 20, 2012 at 14:26
  • it gives me a string that is over 2,600 characters long, here is some of it: /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5Ojf/2wBDAQoKCg0MDRoPDxo3JR8lNzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzf/wAARCABkAIQDASIAAhEBAxEB/8QAGwABAAIDAQEAAAAAAAAAAAAAAAUGAQMEAgf/xAA5EAABAwIEAwUFBQkBAAAAAAABAAIDBBEFEiExQVGREyJhcYEUobHB0QYjMkJSFTNicoOSovDx4f/EABoBAQADAQEBAAAAAAAAAAAAAAACAwQBBQb/xAArEQACAgEDAwIEBwAAAAAAAAAAAQIDEQQSIQUxQVFhExQiMhVCUpGh0fD/2gAMAwEAAhEDEQA/APuCIi4A Commented Mar 20, 2012 at 14:36
  • I tried to decode the base64 string of yours.. But I get an exception, it's only a part of the string you supplied right? Commented Mar 21, 2012 at 8:17
  • yes this is only part of the string, its over 2000 characters long so couldn't post entire string here, won't let me decode it - my application crashes when doing base64.decode(), even when it is surrounded with try catch which is strange Commented Mar 26, 2012 at 13:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.