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?