yydl
yydl

Reputation: 24474

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?)

Upvotes: 1

Views: 1715

Answers (1)

Femi
Femi

Reputation: 64700

Should be eminently reasonable: look at http://developer.android.com/reference/android/util/Base64.html. All you'd need to do is:

  1. Read your icon into a byte[] array on the server.
  2. (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.
  3. Add the contents of the String to the JSON file.
  4. 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.
  5. 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.

Upvotes: 1

Related Questions