Send image data to Android app from App Engine

In my backend of my App Engine, I have a method that retrieves an image from Google Cloud Storage

 @ApiMethod( name = "getProfileImage", path = "image", httpMethod = ApiMethod.HttpMethod.GET) public Image getProfileImage(@Named("imageName")String imageName){ try{ HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); GoogleCredential credential = GoogleCredential.getApplicationDefault(); Storage.Builder storageBuilder = new Storage.Builder(httpTransport,new JacksonFactory(),credential); Storage storage = storageBuilder.build(); Storage.Objects.Get getObject = storage.objects().get("mybucket", imageName); ByteArrayOutputStream out = new ByteArrayOutputStream(); // If you're not in AppEngine, download the whole thing in one request, if possible. getObject.getMediaHttpDownloader().setDirectDownloadEnabled(false); getObject.executeMediaAndDownloadTo(out); byte[] oldImageData = out.toByteArray(); out.close(); ImagesService imagesService = ImagesServiceFactory.getImagesService(); return ImagesServiceFactory.makeImage(oldImageData); }catch(Exception e){ logger.info("Error getting image named "+imageName); } return null; } 

The problem I am facing is how do I get image data when I call this in my Android app?

Since you cannot return primitives from the application kernel, I converted it to Image so that I can call getImageData() in my application to get the byte [].

However, the Image object returned to the application does not match what is in the application, so there is no getImageData ().

How can I get image data in my android app?

If I create an object in which there was a variable byte [], then I set the variable byte [] with string data and return that this object from this method will work?

Update

The image is sent from the Android application. (this code may or may not be right, I have not debugged it yet)

 @WorkerThread public String startResumableSession(){ try{ File file = new File(mFilePath); long fileSize = file.length(); file = null; String sUrl = "https://www.googleapis.com/upload/storage/v1/b/lsimages/o?uploadType=resumable&name="+mImgName; URL url = new URL(sUrl); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); urlConnection.setRequestProperty("Authorization",""); urlConnection.setRequestProperty("X-Upload-Content-Type","image/png"); urlConnection.setRequestProperty("X-Upload-Content-Length",String.valueOf(fileSize)); urlConnection.setRequestMethod("POST"); if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK){ return urlConnection.getHeaderField("Location"); } }catch(Exception e){ e.printStackTrace(); } return null; } private long sendNextChunk(String sUrl,File file,long skip){ int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 524287; long totalBytesSent = 0; try{ long fileSize = file.length(); FileInputStream fileInputStream = new FileInputStream(file); skip = fileInputStream.skip(skip); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); totalBytesSent = skip + bufferSize; buffer = new byte[bufferSize]; bytesRead = fileInputStream.read(buffer, 0, bufferSize); try { while (bytesRead > 0) { try { URL url = new URL(sUrl); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setUseCaches(false); urlConnection.setChunkedStreamingMode(524287); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty("Connection", "Keep-Alive"); urlConnection.setRequestProperty("Content-Type","image/png"); urlConnection.setRequestProperty("Content-Length",String.valueOf(bytesRead)); urlConnection.setRequestProperty("Content-Range", "bytes "+String.valueOf(skip)+"-"+String.valueOf(totalBytesSent)+"/"+String.valueOf(fileSize)); DataOutputStream outputStream = new DataOutputStream(urlConnection.getOutputStream()); outputStream.write(buffer, 0, bufferSize); int code = urlConnection.getResponseCode(); if(code == 308){ String range = urlConnection.getHeaderField("Range"); return Integer.parseInt(range.split("-")[1]); }else if(code == HttpURLConnection.HTTP_CREATED){ return -1; } outputStream.flush(); outputStream.close(); outputStream = null; } catch (OutOfMemoryError e) { e.printStackTrace(); // response = "outofmemoryerror"; // return response; return -1; } fileInputStream.close(); } } catch (Exception e) { e.printStackTrace(); // response = "error"; // return response; return -1; } }catch(Exception e){ e.printStackTrace(); } return -1; } 

Edit 2:

It seems incomprehensible to people that I use Endpoints in my Android app.

+8
android google-app-engine google-cloud-storage
source share
2 answers

What I did / found out in the end is that you need to call execute() in an api call with endpoints and return the real data returned from the API

Example

The api request returns Image

 public Image getProfileImage(@Named("id") long id, @Named("imageName")String imageName){ try{ ProfileRecord pr = get(id); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); GoogleCredential credential = GoogleCredential.getApplicationDefault(); Storage.Builder storageBuilder = new Storage.Builder(httpTransport,new JacksonFactory(),credential); Storage storage = storageBuilder.build(); Storage.Objects.Get getObject = storage.objects().get("mybucket", imageName); ByteArrayOutputStream out = new ByteArrayOutputStream(); // If you're not in AppEngine, download the whole thing in one request, if possible. getObject.getMediaHttpDownloader().setDirectDownloadEnabled(false); getObject.executeMediaAndDownloadTo(out); byte[] oldImageData = out.toByteArray(); out.close(); return ImagesServiceFactory.makeImage(oldImageData); }catch(Exception e){ logger.info("Error getting image named "+imageName); } return null; } 

then on the client side I would call it so as to get it

 Image i = pr.profileImage(id,"name.jpg").execute(); byte[] data = i.decodeImageData(); 
+1
source share

You can use the endpoints of Google Cloud for this:

Google Cloud Endpoints are made up of tools, libraries, and features that allow you to create APIs and client libraries from within an Engine application, called the API backend, to make it easier for the client to access data from other applications. Endpoints make it easy to create a web server for web clients and mobile clients such as Android or Apple iOS.

see https://cloud.google.com/appengine/docs/java/endpoints/

-one
source share

All Articles