Insert byte [] in JSON on Android

I need to send my data using json and send a message 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 can not put this byte array in Json. I use the Json object for Android by default 2.1. When I try to convert byte [] to JSONArray, it gives me an exception in memory.

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

When I try to put an object directly in JsonObject, it returns a weird sixth line in toString () (possibly the memory location of the objects).

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

PS: I first tried using base64. But this often leads to memory exceptions, since the images I have to send and receive are large and therefore base64 is also big!

+4
source share
2 answers

For large files / images, try sending data in a fragment using a fragment . create a bytearray image send a small piece of an array of bytes next time send the next fragment, etc.

+2
source

there is no method that accepts a byte attempt with converting a byte to an int value using type casting

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

Source: https://habr.com/ru/post/1385882/


All Articles