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();
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();
Edit 2:
It seems incomprehensible to people that I use Endpoints in my Android app.