getting image from byte array in JSON object to android app - android

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

Related

use stored JSON instead of Core Data

There are lots of tutorials out there describing how to fetch JSON objects from the web and map them to Core Data.
I'm currently working on an iOS (later: Android as well) app which loads json objects from web and displays them to the user. In my opinion all this mapping from and to Core Data is an overhead in this case, it would be much easier to save the JSON objects directly and use them as "cache" in the app. Are there libraries/documented ways how to achieve fetching json objects, save them locally and fetch them with a predefined identifier?
I would love to fetch e.g. 10 objects, show them to the user and save the data locally. The next time the user is on that list the local data is shown and in the background the json-file is fetched again to be up-to-date. I guess this is a common use case but I didn't find any tutorials/frameworks enabling exactly this.
You can simply use NSURLCache to cache http responses instead saving JSONs
http://nshipster.com/nsurlcache/
There are many ways to implement this. You can implement cache using either file storage or database depending on the complexity as well as quantity of your data. If you're using files, you just need to store JSON response and load it whenever activity/fragment is crated. What I have done sometimes is store the JSON response in the form of string in a file, and then retrieve it on activity/fragment load. Here's an example of reading and writing string files:
Writing files:
FileOutputStream outputStream = context.openFileOutput("myfilename",Context.MODE_PRIVATE);
String stringToBeSaved = myJSONObject.toString();
outputStream.write(stringToBeSaved.getBytes());
Reading from files
FileInputStream inputStream= context.openFileInput("myfilename");
int c;
String temp="";
while( (c = inputStream.read()) != -1){
temp = temp + Character.toString((char)c);
You can convert this string to JSONObject using :
JSONObject jsonObject = new JSONObject(temp);
Or you can use the string according to your needs.

Re-using byte array data from server

I'm having trouble understanding how I can re-use byte data sent down from my server. I'm trying to set up a stub backend for an app and be able to store/return all the backend data locally for a specific user.
When setting a breakpoint and copying the value of my server response (byte[] type), it looks like
[-119, 80, 78, ...]
when copied into a text editor.
If I wanted to store this large array locally and return the value as a byte[] back to my server response handling, how can I accomplish this?
I tried storing the value in a String variable and using getBytes(), but the compiler complained that the string was too large. And I'm not even sure that would have returned what I wanted anyways.
store it as a BLOB in SQLite. In this example --> http://sunil-android.blogspot.com/2013/10/insert-and-retrieve-image-into-db.html BLOB is being used to store an image that has been converted to a byte array. 1mb limit..

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?

Android: System.out: "resolveUri failed on bad bitmap Uri:[B#4072aa78" Prompt when resotre image stream to ListView

I have referred couple of similiar articles regarding the message prompted from System.
The image string is a byte array type Bitmap format. It was encoded to string format by using andorid's Base64 class tool. Then it was saved to mysql DB with Blob format.
On my new App, I want reload the Blob image sting to ListView and show the image: I tried two approaches, but all were failed loading image to listview:
a. put the re-loaded Blob imange(it is a string text type) as value of HashMap's key/value pairs. And then,initial an Adaper (ex. SimpleAdapter) to load the key/vales' and then try to show it on ListView(failed).
b. Similar a, but use Base64 decode method to decode the Blob image back to byte array first. And take it as Byte array type as value of hashMap's key/vale pair.(failed)
I have had studied this problem couple of days and no progress for this problem. If I used wrong process before, Please guide me to correct it, thanks !
By the way. the stored Blob image string can be re-loaded and display to ImageView using
imageview.setImageBitmap(bitmap) method without problem. So, the stored image string data is valid bitmap data. However, this method can not be used on ListView.

Include Small Image in JSON for Android App

My app currently requests a JSON file with some text and other data from my server. I want to add functionality so that it also downloads a very small image (like an icon) through the same file [without creating an additional request]. Is it possible to do so, and how would I go about it (base64?)
Should be eminently reasonable: look at http://developer.android.com/reference/android/util/Base64.html. All you'd need to do is:
Read your icon into a byte[] array on the server.
(Assuming your server is in java) Use something like http://iharder.sourceforge.net/current/java/base64/ to write the byte[] array into a StringOutputStream through http://iharder.sourceforge.net/current/java/base64/api/index.html?Base64.OutputStream.html.
Add the contents of the String to the JSON file.
On the android device call http://developer.android.com/reference/android/util/Base64.html#decode%28java.lang.String,%20int%29 to convert the JSON attribute into a byte[] array.
You can then pass the byte array to http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeByteArray%28byte[],%20int,%20int,%20android.graphics.BitmapFactory.Options%29 or one of its brethren functions (you may have to play with image formats/encodings to get it to swallow your byte array correctly).
Voila! You have a Bitmap you can use.
Let me know how that works out.

Categories

Resources