Download images from byte array in json response - android

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[].

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

ways and usefull libraries for sending image from client (android) to server (Spring)

I find the next possible ways
1) convert Image into Base64 string and put this string into the JSON object.
Send image as json entry android
2) create an array of bytes out of the bitmap, and then create a new string with that array of bytes, and then send it to the server
http://blog.3dmick.com/2012/06/android-app-for-uploading-images-on-server/
Which else libraries or ways do u know ??
Which ways are popular on the enteptise projects?

Decode json response in android

I need to decode a json response sent from php in android.
JSON Response
{"VehicleNumber":"1234FB14","Make":"BMW","Model":"X5","Type":"Car","Color":"Black","EngineCapacity":"1800cc","Fuel":"Diesel","DateOfReg":"31-Dec-2013","OwnerNIC":"B1234567890123","serviceArray":[[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1800","Description":"tponggshmbch"}],[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1900","Description":"tponggshmbch"}]]}
The response consists of values for their respective keys and I can retrieve these value by just calling getString(key) in java. {key:value,2D Array[][]}
The problem is that I can't decode the 2D Array being passed here. I get it in a string format in java and if I display it in my android app I can view this:
[[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1800","Description":"tponggshmbch"}],[{"VehicleNumber":"1234FB14","DateOfService":"07-march-2014","Mileage":"1900","Description":"tponggshmbch"}]]
But i need to retrieve all the value from the 2D array.
Any help please.
If Your json is quite big I recommend using gson or Jackson libraries.

Get Base64 data from handler message in android

send some image data in base64 and collect in hanlder and get message object in handler.
How can collect that image base64 data in handler and how to change that data into byte array after that base64.
Thanks in advance.
To decode the base64 data to a byte array, use this utility.

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