Putting byte[] in JSON on android - android

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!

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.

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]);
}

Related

How to convert ByteString to Image in Android?

I'm trying to convert from ByteString to Image once i receive the ByteString from Socket Connection.
A string which I received from Socket Connection onMessage
[size=302697hex=7c53657276657252657475726e4469723d54454d505f4449527c496d61676546696c654e616d653d496d6167655f3030312e6a70677c030905ffd8ffe000104a…]
How can I get the image from the above ByteString?
I tried to convert this string to Base64 URL, Byte Array but not worked for me.
Please help me to get out of this.
Thanks in Advance.
you need to first convert the hex string to byte array, then decode Base64 (not sure if required), then convert to image. If you can share a full sample string response, I check and verify the steps, and share the code.
you can use https://stackoverflow.com/a/18714790/11535103 to convert the hex string to byte array

Download images from byte array in json response

what would be most efficient way to display a image received as bytearray in json response. I am currently using volley to download the response and use my own caching implementation. Is there a way to use any of the existing libraries like Picasso to display the images ?
The string is a Base64 encoded byte array. Use Base64.decode to decode it to byte[] and then construct a Bitmap from the byte[].

How to decode an array of array elements in Android

A PHP script is sending to an Android app some data in the following format
[{"event_id":"4","message":"test"},["person1","person2"]]
I want to exact the 2 elements from this array into other arrays so I can easily manipulate the data. I got to the point in which the above response from the server is being converted into as string. What I can't seem to be able to do is to parse the data into arrays. I'm trying something on the following lines:
receivedData = new JSONArray(result); //result is the string response from the server
JSONArray array1= receivedData.getJSONArray(0);
JSONArray array2= receivedData.getJSONArray(1);
int len = array1.length();
but lenis not giving me back anything :(
What am I doing wrong and how could I change it.
Many thanks
What if you start by invoking new JSONObject(result); and then pulling an array out of that? I suspect you're trying to pull an array out of something that is not an array. Your PHP should not return something wrapped in [ ] it should return it wrapped in { }... also I believe your third JSON element (the array itself) is just hanging about without a label, which I believe is illegal.
so...
if your php produced this:
{"event_id":"4","message":"test"},"people": ["person1","person2"]}
and your java was this:
JSONObject j = new JSONObject(result);
String [] people = j.getJSONArray("people");
I believe you'd have what you are after.

android Reading image into byte array and last byte of image

Android:
I have two byte arrays one byte array of bitmap and another byte array of String.
I have combine these two byte arrays into 3rd one.
I will write 3rd byte array into storage.
When i read that image which is created from combine byte array of image and string i want to know in byte[] that where image bytes ends and where string bytes gets start.
How do i know the byte representation of end of image byte ? or starting of string byte ?
Thanks in advance.
Before you write out the byte array, you should write out the length of the image part. That way you'll know how much data to read. So your file will look like this:
Length of image (4 bytes, probably - use DataOutputStream for simplicity)
Image data
Text data
(As an aside, I suggest you explicitly write out the text data as UTF-8, and read it as UTF-8. Or you could just use DataOutputStream.writeUTF and DataInputStream.readUTF of course...)

getting image from byte array in JSON object to android app

Heres my situation:
I have a RESTful WCF service running on my server, the service is meant to get various types of data about people from a database and makes that data available as a single JSON object. It works great!
[EDIT]There is another service that maintains an image cache in the file system on the server. When a request is sent to the RESTful service, that service then requests an image from the image service. If the image is already in the cache (same width, height and person as the request), it returns that (as a byte array). We MUST use this service to retrieve images.
Now what I want is the image of that person. In our database, the image is a long raw (ew). However, I have dealt with that issue already (previous paragraph). The image is now a Byte array. I am pretty new to android and I am not sure what the best way to retrieve this image is. What I thought I could do was add the byte array to the JSON object and then use the base64 decoder to convert it into a drawable image. However, everytime I try, it times out and tells me it expected ',' or ']' at some arbitrary index of the char buffer for the JSON object.
I have been able to pull the small bits of data out of the JSON object without an issue, but now that there is a huge byte array in it, the JSONObject hates me. What would be a better way to get this image from my service?
Base64 encode the byte array to get a string.
Add the string to JSON object and send it.
When JSON is received, get out the string.
Base64 decode it to get back the byte array.
Use byte array to create Image.
See this question on storing images, it's always better to store this sort of data on file system. If possible deprecate that field, and create a script to move existing images to file system.
You should then store the images on a file system (or some sort of content management system) which can be retrieved by a URL.
Then store the URL in the database. you can then send this in your json object.
{
...
image_url:<url from database>
...
}
When the client receives this it will make a call to that URL and download the image.
Your client will have to make a separate call to retrieve the image but it's generally better than filling your database with binary data. This can also work to your advantage if you want to display data fast while allowing the image to be downloaded in the background.
Better than using Base64 encoding is this way of returning Stream (from WCF RAW programming)
[OperationContract, WebGet]
public Stream GetTestImage(Image image)
{
MemoryStream stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Position = 0;
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
return stream;
}

Categories

Resources