从App Engine将图像数据发送到Android应用程序

在我的App Engine后端,我有一种从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; } 

我遇到的问题是如何获取图像数据,当我在我的android应用程序中调用?

既然你不能从应用程序引擎返回原语,我把它转换成Image以便我可以在我的应用程序中调用getImageData()来获取字节[]。

但是,返回到应用程序的图像对象是不一样的应用程序引擎,所以没有getImageData()。

我怎样才能获得图像数据到我的android应用程序?

如果我创build一个对象,它有一个byte []variables,然后我设置byte []variables的string数据,并返回该方法将工作的对象?

更新

图像从android应用程序发送。 (这段代码可能不对,我还没有debugging过)

 @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; } 

编辑2:

显然它不清楚人们,我在我的Android应用程序中使用端点

Solutions Collecting From Web of "从App Engine将图像数据发送到Android应用程序"

我最终做了什么/发现你需要调用与端点的API调用execute() ,它返回从API传回的真实数据

api调用返回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; } 

那么在客户端,我会这样称呼它

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

您可以使用Google Cloud Endpoints执行以下操作:

Google Cloud Endpoints由工具,库和function组成,允许您从App Engine应用程序(称为API后端)生成API和客户端库,以简化客户端对其他应用程序的数据访问。 端点使得为Web客户端和移动客户端(如Android或Apple iOS)创buildWeb后端变得更加容易。

请参阅https://cloud.google.com/appengine/docs/java/endpoints/