7

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?

Matt Grogan
  • 317
  • 1
  • 6
  • 18

3 Answers3

10
  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.
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
0

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

Karel Kral
  • 5,297
  • 6
  • 40
  • 50
0

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.

Community
  • 1
  • 1
rogermushroom
  • 5,486
  • 4
  • 42
  • 68