Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting image from byte array in JSON object to android app

Tags:

json

android

wcf

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?

like image 702
Matt Grogan Avatar asked Jan 04 '12 21:01

Matt Grogan


1 Answers

  1. Base64 encode the byte array to get a string.
  2. Add the string to JSON object and send it.
  3. When JSON is received, get out the string.
  4. Base64 decode it to get back the byte array.
  5. Use byte array to create Image.
like image 152
Peter Knego Avatar answered Sep 21 '22 18:09

Peter Knego