How to transfer Bitmap from Android to Google App Engine Servlet?

I have been fighting this problem for several days, and you are my last chance to solve it.

Purpose:

To upload a bitmap from an Android client to the Google engine engine and save it to the data warehouse.

  • I use serialization to transfer an object from client to server and vice versa.

What I tried:

  • sending bitmap
    • but i got java.io.NotSerializableException: android.graphics.Bitmap
  • then I tried to create from Bitmap Blob on the client, like this:

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
    Byte[] bArray = bos.toByteArray();
    Blob blob = new Blob(bArray); 
    

    But on the client side, I don’t have Google engine classes (for Blob), so I tried reinstalling it by extracting a specific class, but it created new problems.

So, I ask to what extent I can go to solve this problem.
Thank you

0
source share
3

:

  • Java JVM. Java JVM ( ).

  • , HTTP POST Content-Type (, application/octet-stream).

, :

+3

:

GAE appengine .

Android :

  • URL- GAE
  • GAE blobkey.
  • Later, use blobkey to serve the image to your customers.

GAE Servlet Code:

getUploadURL:

BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String url = blobstoreService.createUploadUrl(path_to_your_upload_servlet);

uploadServlet - saves to blobstore, returns blobkey to bootloader

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 :

String uploadUrl = getUrlAsString(..your getUrl servlet path...)
// Upload to GAE (include apache-mime4j.jar and httpmime.jar in your project for this code)
File file = new File(imageFilePath);
HttpPost postRequest = new HttpPost(uploadUrl);
MultipartEntity entity = new MultipartEntity();
entity.addPart("file", new FileBody(file));
postRequest.setEntity(entity);
HttpResponse httpResponse;
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setBooleanParameter("http.protocol.handle-redirects",false);

httpResponse = httpClient.execute(postRequest);
int status = httpResponse.getStatusLine().getStatusCode();
String blobKey = getInputStreamAsString(httpResponse.getEntity().getContent())
0
source

All Articles