通过云端点将图像传输到/从Android应用程序到应用程序引擎的后端 [英] Transferring images to/from Android App through Cloud Endpoints to App Engine back-end

查看:127
本文介绍了通过云端点将图像传输到/从Android应用程序到应用程序引擎的后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个应用程序,需要的图像传递,并从一个App Engine的后端。本来我计划将图像发送(他们都只是小 - 最大100KB - 平均20KB)直接通过但是发送的数据通过我收到一个JSON错误(来自REST API)的端点的字节数组当端点,声明该数据有无效字符。有没有解决的办法吗?

I'm currently working on an app that requires images to be passed to and from an App engine back-end. Originally I planned on sending the images (they are only small - max 100kb - average 20kb) directly through the endpoint however when sending the data as a byte array through the endpoint I receive a JSON error (from the rest API) stating that the data has an invalid character. Is there a way around this?

我的第二次​​尝试是使用使用BlobService和使用下面的code返回上传的URL给客户端:

My second attempt was to use use the BlobService and return an upload URL to the client using the below code:

BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
UploadOptions uploadOptions = UploadOptions.Builder.withGoogleStorageBucketName("bucketname").maxUploadSizeBytes(1048576);              
String url = blobstoreService.createUploadUrl("/uploaded", uploadOptions);

然后,使用Android设备上的HTTP后上传的图片:

Then using a HTTP post on the android device to upload the image:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("data", new ByteArrayBody(data,"image/png","img.png"));
httppost.setEntity(entity);
String res = EntityUtils.toString( httpclient.execute(httppost).getEntity(), "UTF-8");

这似乎工作和图像上传。不过,我不知道如何得到这个上传图像blobkey。有谁知道?此外,从HTTP后的结果是一个404错误 - 因为/上传页面不存在我猜

This seems to work and the image is uploaded. However, I have no idea how to get the blobkey of this uploaded image. Does anyone know? Also, the result from the HTTP post is a 404 error - because the "/uploaded" page does not exist I'm guessing?

第三,在手动键入的blobkey,并用它来恢复和图像服务的URL与此code:

Thirdly, when manually typing in the blobkey and using it to return and image serving url with this code:

    private String getImage(){
        return getThumbUrl(new BlobKey("encoded_gs_key:ZGNpbWcxMy93czZwZ2lUeXdpY0xvZ2xtZGpHZ2dn")); 
    }

    private String getThumbUrl(BlobKey blobkey){       
        ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(blobkey);
        try {
            return ImagesServiceFactory.getImagesService().getServingUrl(options);
        } catch(IllegalArgumentException e) {
            e.printStackTrace();
            return null;
        } catch(ImagesServiceFailureException e) {
            e.printStackTrace();
            return null;
        }
    }

我收到然而,图像的颜色都搞砸了图像的URL。我上传的索引PNG图片...我不知道,如果ImageService能正确处理它们。如果它不能,我怎么去服务于形象直接,即不是通过ImageService而是通过BlobstoreService.serve()?

I receive the URL of the image however the image colors are all messed up. I am uploading indexed pngs... I'm not sure if the ImageService can handle them correctly. If it cant, how do I go about serving the image directly i.e. not through the ImageService but through BlobstoreService.serve()?

下面是从ImageService URL产生的画面的图像: http://i.imgur.com /EhfkJ9j.png

Here is an image of the resultant picture from the ImageService URL: http://i.imgur.com/EhfkJ9j.png

干杯, 本

推荐答案

关于BLOB键,当你创建上传网址你通过你的应用程序引擎页面的上传后,被调用的名称paramenter。你需要实现该页面(上传)的https://developers.google.com/appengine/docs/java/blobstore/#Java_Uploading_a_blob

About the blob key, when you create the upload url you pass a paramenter with the name of your appengine page that gets called after upload. You need to implement that page ("uploaded") https://developers.google.com/appengine/docs/java/blobstore/#Java_Uploading_a_blob

这篇关于通过云端点将图像传输到/从Android应用程序到应用程序引擎的后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆