简体   繁体   English

Android从Google端点将图像上传到Blobstore

[英]Android upload image to Blobstore from Google Endpoints

I use Google Endpoints for my Android application. 我将Google Endpoints用于我的Android应用程序。 I want to upload an image to Blobstore: 我想将图像上传到Blobstore:

Endpoint to get upload url: 端点获取上传网址:

@ApiMethod(name = "generateImageUploadUrl")
    public UploadUrl generateImageUploadUrl() {
        BlobstoreService blobstoreService =
                BlobstoreServiceFactory.getBlobstoreService();

        String blobUploadUrl = blobstoreService.createUploadUrl("/blob/upload");
        return new UploadUrl(blobUploadUrl); // UploadUrl is an inner class to store the url
    }

Code in Android app to upload image: Android应用中的代码以上传图片:

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(url);
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        try{
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 75, bos);
            byte[] data = bos.toByteArray();
            ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
            reqEntity.addPart("picture", bab);

            postRequest.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(postRequest);
            HttpEntity entity = response.getEntity();
            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();
            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }

            s.toString();
        }
        catch(Exception e){
            e.getCause();
        }

How do I get the final URL of the image? 如何获得图像的最终URL?

Once the file is uploaded to the blobstore you can generate and return the serving URL as JSON like this: 将文件上传到Blobstore后,您可以生成并以JSON形式返回服务网址,如下所示:

public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException  {
    List<BlobKey> blobs = blobstoreService.getUploads(req).get("file");
    BlobKey blobKey = blobs.get(0);
    ImagesService imagesService = ImagesServiceFactory.getImagesService();
    ServingUrlOptions servingOptions = Builder.withBlobKey(blobKey);
    String servingUrl = imagesService.getServingUrl(servingOptions);

    res.setStatus(HttpServletResponse.SC_OK);
    res.setContentType("application/json");

    JSONObject json = new JSONObject();
    json.put("servingUrl", servingUrl);
    PrintWriter out = res.getWriter();
    out.print(json.toString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Android上传文件(图片)并使用GAE和Google端点保存到Blobstore? - How do I upload a file(picture) from android and save to the blobstore using GAE and google endpoints? 从Android对Google App Engine Java Blobstore上传无响应 - No Response on Google App Engine Java Blobstore Upload from Android 从Android将文件上传到GAE blobstore(Python) - Upload file to GAE blobstore (Python) from android 在Android中上传到Appengine Blobstore - Upload to Appengine Blobstore in Android 从Android客户端将图像存储到Blobstore并检索blobkey并上传URL以存储在Datastore中。 - GAE - Store image to Blobstore from android client and retrieve blobkey and upload url to store in Datastore. - GAE 从Android发送的不仅仅是图片到Blobstore的上传网址? - Sending more than just an image to blobstore's upload url from Android? 如何从Google Blobstore下载文件到Android - how to download file from google blobstore to android Android:从Blobstore图片获取缩略图 - android: get thumbnail from blobstore image 在Android中使用带有HttpURLConnection的Blobstore API上传到Google Cloud Storage - upload to Google Cloud Storage using Blobstore API with HttpURLConnection in Android 从Android获取Blobstore上传网址的语法是什么? - What is the syntax to get a Blobstore upload url from Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM