Include Small Image in JSON for Android App - android

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.

Related

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..

Save Image files and some text together as one file/object

I created an android test app with camera interface in which I want to save a text message and image file as one object. I am able to enter message, call the camera,take a picture and also populate the image in the image view on the app. Now I want to save them as one single record/object so that I can transfer it over network using protocol such as ftp. How to save image file and text together as one single file/object? Could someone please tell me how to do that. Thanks in advance.
There are lots of possibilities, and it depends to some extent on what will be consuming the file after it is transferred. You could, for instance, simply serialize the string and the image data using a DataOutputStream wrapped around a FileOutputStream. As a fancy version of that, you could define a class to contain the text and the image, have that class implement Serializable, and serialize it to a file. Alternatively, you could serialize the image data as a base-64 string and then put the text and the base-64 image data into an XML document or JSON string. Other approaches are also possible.
If you provide more details about what kind of process will consume this file after it has been transferred, perhaps we can provide more focused suggestions.

How to random read raw resource file in Android

In my Android project, I have a 2M-bytes raw data file. Since my application is a long-life app, I don't want it to always seize 2M memory. The data file has been formatted, once I need to some data from the data file, I just need to seek to some position and read several bytes.
The Resource class can only return an InputStream on raw file, but InputStream cannot do random read.
Is there a way on Android to random read some bytes from the raw data file? Or I have to read the entire file into memory when I only need a few bytes.
InputStream can skip bytes with skip() can also mark an offset with mark(), on reset() it can go back to marked position. All that can be used to do random IO.
You can store byte offsets in a separate lookup file as well.
Android is built upon Java so take a look at this tutorial:
http://docs.oracle.com/javase/tutorial/essential/io/rafs.html

getting image from byte array in JSON object to android app

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

Technique for Storing Android Contact Icons

I'm wondering what the best way to store a local phone contact's icon is. I'm writing a manager that will allow the user to select contacts for display in a list which now needs contact icon display.
Would storing the icon in a different location on select work or should I try the route of storing the location of the icon and linking to it that way? Can anyone who has went through this already point me in the right direction? I'm using an Sqlite database to store the contacts already.
Any code/links would be very helpful.
Since you are already storing the contacts in an Sqlite db, I'd just add another field to that db that will hold an encoded image.
The way I have gone to solve a similar issue is: I used Base64 for encoding an image into a String and then I store that String wherever I want...
I added one function to the Base64 class to directly encode a Bitmap object for me and return a String, here's the code:
public static String encodeBitmap(Bitmap bmp) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] buf = stream.toByteArray();
return encodeBytes(buf);
}
where encodeBytes(buffer) is already an implemented function of the Base64 class.
This would be a better way of doing it than storing the path to the image, because a user can easily change the path and then your application wouldn't find the picture anymore.
Hope that helps.
First, I'd recommend looking at the Android source and seeing how they do it.
That aside, if the images are smallish, I'd highly recommend creating a proper ContentProvider and storing the images as binary blobs attached to the row using Cursor.getBlob(). See http://developer.android.com/intl/de/guide/topics/providers/content-providers.html#querying for more details.
For larger images, take a look at How to store large blobs in an android content provider?

Categories

Resources