Getting an image from an array of bytes in a JSON object for an Android application

Here is my situation:

I have a RESTful WCF service running on my server, this service is designed to receive various types of people data from a database and makes this data available as a single JSON object. It works great!

[EDIT] There is another service that supports image cache in the file system on the server. When a request is sent to the RESTful service, this service then requests an image from the image service. If the image is already in the cache (the same width, height and person as the request), it returns this (as an array of bytes). We MUST use this service to retrieve images.

Now what I want is an image of this person. In our database, the image is long raw (ew). However, I have already addressed this issue (previous paragraph). Now the image is an array of bytes. I'm new to android and I'm not sure what the best way to get this image. What I thought I could do was add an array of bytes to the JSON object, and then use a base64 decoder to convert it to a drawn image. However, every time I try, time runs out and tells me that it was expecting a ',' or ']' at some arbitrary char buffer index for the JSON object.

I managed to pull small pieces of data from the JSON object without problems, but now that it has a huge array of bytes in it, JSONObject hates me. What would be the best way to get this image from my service?

+7
source share
3 answers
  • Base64 encodes an array of bytes to get a string.
  • Add a string to the JSON object and submit it.
  • When the JSON is received, exit the line.
  • Base64 decodes it to return an array of bytes.
  • Use an array of bytes to create the image.
+10
source

See this question on storing images ; it is always better to store such data in the file system. If possible, discard this field and create a script to move existing images to the file system.

Then you must save the images in the file system (or some kind of content management system), which can be obtained at the URL.

Then save the url in the database. you can send this to your json object.

{ ... image_url:<url from database> ... } 

When the client receives this, he will make a call to this URL and upload the image.

Your client will have to make a separate call to retrieve the image, but it is better than populating the database with binary data. It may also work to your advantage if you want to quickly display data, allowing you to download an image in the background.

0
source

Better than using Base64 encoding, this is a way to return a 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; }
[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; } 
0
source

All Articles