How To Stream Bitmap From Android To Google App Engine Servlet?
Solution 1:
Few notes:
Do not use Java serialization to transfer data between JVMs. Java serialization is not standardized and is not guaranteed to be compatible between JVMs (or even between versions).
To send binary data it's best to use HTTP POST and set
Content-Type
appropriately (e.g.application/octet-stream
).
So, to make this work do this:
Create a servlet which handles POST and gets the binary data. Use
servletRequest.getInputStream()
to get hold of binary data.Use Blobstore FileService API to save data to blobstore.
On Android side use a http client to make a POST request and add your bitmap's binary data to it. If you need to add some metadata use Http headers.
Solution 2:
This might be useful
How to upload and store an image with google app engine (java)
Alternatively, you can try blobstore api
http://code.google.com/appengine/docs/java/blobstore/overview.html
Solution 3:
Here is a production tested way:
Use GAE appengine to upload your bitmap to, and serve for future clients.
On the Android code, follow these steps:
- Get an Upload URL from GAE
- Upload your bitmap to GAE, and get a blobkey back
- Later on, use the blobkey to serve the image to your clients.
GAE Servlet code:
getUploadURL:
BlobstoreServiceblobstoreService= BlobstoreServiceFactory.getBlobstoreService();
Stringurl= blobstoreService.createUploadUrl(path_to_your_upload_servlet);
uploadServlet - stores in blobstore, returns the blobkey to the uploader
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
Map<String, List<BlobKey>> uploads = blobstoreService.getUploads(request);
String fileName = uploads.keySet().iterator().next();
final BlobKey blobKey = uploads.get(fileName).get(0);
response.getWriter().println(blobKey.getKeyString());
Android client code:
StringuploadUrl= getUrlAsString(..your getUrl servlet path...)
// Upload to GAE (include apache-mime4j.jar and httpmime.jar in your project for this code)Filefile=newFile(imageFilePath);
HttpPostpostRequest=newHttpPost(uploadUrl);
MultipartEntityentity=newMultipartEntity();
entity.addPart("file", newFileBody(file));
postRequest.setEntity(entity);
HttpResponse httpResponse;
HttpClienthttpClient=newDefaultHttpClient();
httpClient.getParams().setBooleanParameter("http.protocol.handle-redirects",false);
httpResponse = httpClient.execute(postRequest);
intstatus= httpResponse.getStatusLine().getStatusCode();
StringblobKey= getInputStreamAsString(httpResponse.getEntity().getContent())
Post a Comment for "How To Stream Bitmap From Android To Google App Engine Servlet?"