4
votes

i have to send my data using json and have to send image using byte[] in my json. Now I can get the bitmap to convert to bytearray using the following line of codes:

selectedImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100,stream);
byte[] byteArray = stream.toByteArray();

But I cant put this byte array in Json. I am using android 2.1 default Json object. When I try to convert byte[] to JSONArray, it gives me Out of memory exception.

JSONArray jsonArray = new JSONArray();      
for(int i=0;i<byteArray.length;i++) {
    jsonArray.put(byteArray[i]);
}    

When I try to put the object directly in JsonObject, it returns a weird hex string in toString() (probably the objects memory location).

Can anybody please help me out with this. Is there a standard way to put a byte[] in json??

P.S: I tried using base64 initially. But that frequently cause Out of Memory exceptions as the images i have to send and recieve are large and thus there base64 is large too!

2

2 Answers

2
votes

For big files/images try to send the data in chunk by chunk basis. create bytearray of the image send small chunk of byte array next time send next chunk and so on.

0
votes

there is no such method which accept the byte try with the convert the byte to int value using type casting

JSONArray jsonArray = new JSONArray();      
for(int i=0;i<byteArray.length;i++) {
    jsonArray.put((int)byteArray[i]);
}