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
1.You can encode the byte[] to base64 using:
http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html
2.Send that data with a HTTP POST request to your AppEngine servlet.
3.Configure AppEngine to accept a servlet.
4.Then you have the choice to save it to the Datastore or the Blobstore. -I prefer the blobstore for these kinds of things.
5.Decode the base64 string server side.
6.From there on you'l need to cut up your string into smaller pieces and write each piece to the blobstore.
Here's some code for writing it to the blobstore.
byte[] finalImageArray = null;
try {
finalImageArray = Base64.decode(finalImageData.getBytes()); //finalImageData is the string retrieved from the HTTP POST
} catch (Base64DecoderException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile("image/png");
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
int steps = (int) Math.floor(finalImageArray.length/1000);
int current = 0;
for(int i = 0; i < 1000; i++){
writeChannel.write(ByteBuffer.wrap(Arrays.copyOfRange(finalImageArray, current, steps+current)));
current = current + steps;
}
writeChannel.write(ByteBuffer.wrap(Arrays.copyOfRange(finalImageArray, current, finalImageArray.length))); //The reason it's cut up like this is because you can't write the complete string in one go.
writeChannel.closeFinally();
blobKey = fileService.getBlobKey(file);
if(blobKey == null)
blobKey = retryBloBKey(file); //My own method, AppEngine tends to not return the blobKey once a while.
}
catch(Exception e){
logger.log(Level.SEVERE,e.getMessage());
}
return blobKey.getKeyString();
Write a servlet where you retrieve the image data with the provided key.
Enjoy your beautiful code :)
//The reason i save it in binary is because that gives me options to play with the image api, you can also choose to save it in the base64 format.