Send and receive image via WebService

4

I'm developing an app where I need to convert an image to JSON and send it to a WebService. Afterwards I will need to perform the inverse path, that is, receive a JSON image from a WebService and present it to the application user.

Well, to send the image I'm using the following code:

 ByteArrayOutputStream stream = new ByteArrayOutputStream();
 bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
 byte[] imgByteArray = stream.toByteArray();

 String imgArray = Base64.encodeToString(imgByteArray, Base64.DEFAULT);
 JSONObject jsonImg = new JSONObject().put("imgByteArray", imgArray);

So far, it's okay.

To receive the image I'm using the following code:

 JSONObject response = new JSONObject(responseJSON);
 String imgBytes = responsegetString("imagem");

 byte[] imgRecebida = Base64.decode(imgBytes, Base64.DEFAULT);
 Bitmap bitNew = BitmapFactory.decodeByteArray(imgRecebida, 0, imgRecebida.length);

However, the answer I have in logcat is:

 java.lang.IllegalArgumentException: bad base-64

And if I change Base64.decode to:

final byte[] imgRecebida = Base64.decode(imgBytes.getBytes(), Base64.DEFAULT);

I get as a result:

 D/skia﹕ --- SkImageDecoder::Factory returned null

Does anyone know why?

    
asked by anonymous 20.05.2014 / 06:41

1 answer

1

The code shown is right, the only difference to what I used at some time was that instead of 100 I used 50 (% with%), because I did not need much quality. a good test would be to get the string that is on the server and use that site link .

    
28.05.2014 / 04:48