Reputation: 6469
I have this app in which I have to receive an image in the form of a byte array. That image has been sent to the server, previously, in this way:
image = image.createScaledBitmap((Bitmap) extras.get("data"), 380, 400, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imagen.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
The image is sent OK to the server. Another part of the application calls the server to get a JSON in which the image is enclosed. I store it in an String, and then I use string.getBytes()
to get the byte array, something like that:
byte[] array=stringimage.getbytes();
The array is something like: 119,80,78,71,13,10...
I think now "ok, I use now BitmapFactory,decodeByteArray and get my Bitmap" but it returns null. I start Googling and looking here in Stack Overflow and I see various approaches to the problem. One of that:
image = Bitmap.createBitmap(380, 400, Bitmap.Config.RGB_565);
int row = 0, col = 0;
for (int i = 0; i < array.length; i += 3) {
image.setPixel(col++, row, array[i + 2] & array[i + 1] & array[i]);
if (col == 380) {
col = 0;
row++;
}
}
That is something like decode the image and set the pixels by hand.
It doesn't work.
Another one:
byte[] array=Base64.decode(fotostring.getBytes(),Base64.DEFAULT);
It doesn't work
My question is: How must I ask my mate, who is in charge of the server side, to send me the array? in which format? he does not touch the image I send him previously in any way. Does he need to do?
Or, how I must manage the byte array? Do I need no "translate" to another format, one that decodeByteArray can understand?
Upvotes: 0
Views: 157
Reputation: 15774
The problem you are facing is because of the encoding / decoding. The byte array that you received from the image is binary and cannot be interpreted as text. For transferring it over a text-based protocol (I assume you would have used HTTP), you will have to encode it into a textual format. The symmetrical operation need to be performed when you receive it from the server.
When you send it to the server, encode in a format (say Base64) and use the same format to decode the received string.
Upvotes: 1