1

I'm trying for educational purposes, use the camera of Android to get a photo and send this to a webservice using SOAP.

First, the camera send me a byte[] array but this isn't serializable and I can't send this to the webservice. Now I'm trying to convert this into a String (this type is serializable), and in the webservice reconvert this String into a byte[] array.

How I can do it?

For example, I'm trying:

request.addProperty("photo", (Arrays.toString((byte[])params[0])));

How I get this String and convert to the original and exactly byte[]?

Thanks in advance!

jenzz
  • 7,239
  • 6
  • 49
  • 69
Hypnotize
  • 143
  • 2
  • 17
  • 3
    Would it not be easier to turn it into an image format and serialize that? – Neil Townsend Jun 16 '13 at 20:04
  • `byte[]` isn't serializable? Serializing is precisely converting to `byte[]`... – m0skit0 Jun 16 '13 at 20:06
  • I suggest you use `byte[]` not `Byte[]` as the latter is less efficient. BTW byte[] is Serializable, you can just sent it as it is. – Peter Lawrey Jun 16 '13 at 20:06
  • Thanks for faster answers. When I try to send byte[] Android... 06-16 22:10:18.787: W/System.err(19363): java.lang.RuntimeException: Cannot serialize: [B@41180398 – Hypnotize Jun 16 '13 at 20:10

3 Answers3

2

If you really need to convert a byte[] into a String, use an intermediate serialization meant for arbitrary byte data, such Base64. Strings are for character data, not arbitrary byte[]s.

Android's Base64 class makes the above trivial.

This question is pretty much a duplicate of Android: using Base64 to encode an Image to String.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

If your web-service only supports ASCII input you can encode your data in base 64: Base64 Encoding in Java

Community
  • 1
  • 1
Pragmateek
  • 13,174
  • 9
  • 74
  • 108
0

Finally I use it to send byte[] to a Webservice and Decode it. Thanks!

ANDROID SOAP SERVICE:

request.addProperty("photo", Base64.encodeToString((byte[])params[0], Base64.DEFAULT));

SOAP WEBSERVICE

BASE64Decoder decoder = new BASE64Decoder();                    
byte[] a = decoder.decodeBuffer(VAR);
Hypnotize
  • 143
  • 2
  • 17