I want to send images over a web service to an android device. This web service is written in VB.net and it is being called by android device to fetch the data. I can fetch the data normally, but the issue is with Images. I have converted image into byte array, converted it into string and passed it over web service(XML) to android device. I cannot decode this String byte array back to an image on android side. How to achieve this? Any simple solution? Am I using wrong approach?
Asked
Active
Viewed 1.7k times
2
-
Why can't you? What problems are you having? What methods have you applied to this problem? What's the XML look like when it gets to the device. – ChiefTwoPencils May 08 '15 at 08:57
-
"converted it into string" - How? What string representation do you use? – JimmyB May 08 '15 at 08:58
-
Bytes to String conversion is wrong. You will lose few bytes when convert it back to bytes. Need to find some other way – Pasupathi Rajamanickam May 08 '15 at 08:59
-
@ChiefTwoPencils The real problem is when I write the raw byte array on a file and put it manually into Assets Folder of android, It works. But we cant directly pass byte array over XML right. So I converted it into String and sent it. But the length of this String is twice the length of Byte array, hence its not getting decoded on android side. – Dropkick54 May 08 '15 at 09:02
-
http://www.codejava.net/java-ee/web-services/java-web-services-binary-data-transfer-example-base64-encoding This may help. – Pasupathi Rajamanickam May 08 '15 at 09:03
-
@Pasupathi how should i send the Byte array then? I want to send Byte array of the Image along with some Text data. Basically I just want to send Images to android. is there any simple way? – Dropkick54 May 08 '15 at 09:03
-
@HannoBinder We have tried all the available String representations. – Dropkick54 May 08 '15 at 09:08
-
Then it seems like you failed to use correctly all of them. – JimmyB May 08 '15 at 09:09
-
https://msdn.microsoft.com/en-us/library/aa528822.aspx. You can refer this for VB.NET. Send the image as byte data. For Text if it is very few, you can use response header to return back the text data. – Pasupathi Rajamanickam May 08 '15 at 09:09
-
@HannoBinder it might be true but we have been stuck over it for like 1 week now. Which representation you reckon might work? We will use that thing again. – Dropkick54 May 08 '15 at 09:11
-
As @Peanut suggested, you should probably be using Base-64. Or, see if the SOAP framework you're using can't handle byte arrays for you, which is not too uncommon. – JimmyB May 08 '15 at 09:13
-
Another approach could be to just return a URL via the WS and have the client download the image via HTTP from the URL. Could be more efficient too, because binary data inside XML has a lot of overhead. – JimmyB May 08 '15 at 09:16
-
@HannoBinder will give it a shot. But does android convert the Data from an XML file into some encoding of its own? We are not getting the same data on android side. Hence it gives null when converting bytes to BitMap – Dropkick54 May 08 '15 at 09:17
-
@Pasupathi problem is this Byte array is changing its Form over XML and hence we cannot read it on android side. – Dropkick54 May 08 '15 at 09:20
-
A *string* passed to Android via SOAP should become the exact same *string* after reception. Then, if you have a routine that can correctly decode binary data out of a string, that should work. (`String.getBytes()` is *not* what you want here.) – JimmyB May 08 '15 at 09:21
-
@HannoBinder So you mean I should not convert the image into Byte array? I should convert it into Binary and send it over XML to android side and decode this Binary Into Image there? – Dropkick54 May 08 '15 at 09:23
-
I think what you need to do is: 1. convert the image to a `byte[]` by appropriate means, 2. have the `byte[]` converted to a Base64 string, 3. put this string into your SOAP-XML, 4. on Android, get the string from the XML, 5. have the Base64 string converted back to a `byte[]`, 6. create an image object from the binary data in the `byte[]`. Should be pretty fool-proof, if you have a working Base64 en/decoder and the binary format of the source image can be processed on the Android device. – JimmyB May 08 '15 at 10:42
-
Maybe see http://stackoverflow.com/a/12081228/1015327 – JimmyB May 08 '15 at 10:58
4 Answers
5
Converting a byte array to a String is a bit tricky. Since the image data may contain null bytes, the String cannot. You probably need to encode the byte array so that the String is valid. Try to use base64-encoding.
Consider the following code example:
System.Convert.ToBase64String(byteArray)
You need to decode the base64-encoded String in android as well. Here is an example for that as well:
import org.apache.commons.codec.binary.Base64;
String base64String = "... your base64 String ...";
byte[] decodedBytes = Base64.decodeBase64(base64String.getBytes());
decodedBytes
will contain your original data.

Peanut
- 3,753
- 3
- 31
- 45
-
I tried this approach. I think that android By default reads the data in UTF8 encoding. I got the error like "This character is not valid Base64 character" – Dropkick54 May 08 '15 at 09:07
-
1@Peanut to avoid the confusion I see coming up, maybe you should change the example to handle a generic `byte[]` instead of a string converted to bytes converted to a string. – JimmyB May 08 '15 at 09:17
-
@Peanut byte[] encodedBytes = Base64.encodeBase64(imageByteArray[i].getBytes()); byte[] decodedBytes = Base64.decodeBase64(encodedBytes); Bitmap bMap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length); bMap still returns Null – Dropkick54 May 08 '15 at 10:15
-
You only accessed the first byte of the array... And I still had the encode function in the code which was not needed there. Try this: `byte[] decodedBytes = Base64.decodeBase64(imageByteArray);` `Bitmap bMap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);` – Peanut May 08 '15 at 12:46
4
For Converting Image To String :
String imageAsString=Base64.encodeToString(byteArray, Base64.DEFAULT);
For Converting String To Image:
byte[] imageAsBytes = Base64.decode(imageAsString.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView)findViewById(R.id.ImageView);
Bitmap imgBitmap=BitmapFactory.decodeByteArray(imageAsBytes,0,imageAsBytes.length);
image.setImageBitmap(imgBitmap);

Sudarshan Vidhate
- 133
- 8
-
-
@voipp Base64 class is already included with Android SDK, so there is no need to add other logic externally. – Sudarshan Vidhate Mar 05 '17 at 12:24
0
We got the solution
On webservice side we used:
System.Convert.ToBase64String(byte_arr)
On Android Side we used:
byte[] decodedBytes = Base64.decode(imageByteArray[i].getBytes(),Base64.DEFAULT);
Bitmap bMap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
Thank you for help. Cheers!!!

GitaarLAB
- 14,536
- 11
- 60
- 80

Dropkick54
- 283
- 1
- 2
- 9
0
You can use org.castor.util.Base64Encoder to encode byte array to String
byte [] doc = {37, 80, 68, 70, 45, 49, 46};
String image = new String(Base64Encoder.encode(doc));
In your web service class, use this
byte[] imageData = Base64Decoder.decode(image);

gathila
- 31
- 5