I am trying to write an Android application that will take a picture, put the data (byte[]) in an object along with some metadata, and post that to an AppEngine server where it will get persisted in a datastore as a blob. I don’t really want to save the image as a file on Android (unless it’s absolutely necessary). I searched around for solutions but nothing clear or specific enough came up. My questions are:
- how do I post the object to my servlet? Specifically how to properly serialize the object and get the serialized output to an HttpPost or something similar.
- once I persist the blob in the datastore, how will I be able to retrieve it as an image to display on a webpage?
Code examples would be very helpful. Also, if the approach I am taking is too complicated or problematic, please suggest other approaches (e.g. saving image as file, posting to server, then deleting).
You just need to do a Http-FileUpload which in a special case of a POST.
There is no need to uuencode the file.
No need to use a special lib/jar
No need to save the object to disk (regardless that the following example is doing so)
You find a very good explanation of Http-Command and as your special focus “file upload” under
Using java.net.URLConnection to fire and handle HTTP requests
The file upload sample from there follows (watch “send binary file”)
and it is possible to add some companion data either
Regarding the second part of your question. When successful uploading the file (I use apache common files), it is not a big deal to deliver a blob as an image.
This is how to accept a file in a servlet
And this code delivers an image
I hope this code fragments help to answer your questions