如何使用MediaHttpUploader将图像上传到BlobStore?

时间:2012-07-08 15:20:11

标签: android google-app-engine google-api google-api-java-client

我在android中使用以下函数,但在调用uploader.upload()时出现错误“500 Java堆空间”。

如果我在uploader.setDirectUploadEnabled(true)之前致电uploader.upload(),我会在upload()上收到错误“411长度”,而不是错误500.

public static String uploadImage(Context context, String file, MediaHttpUploaderProgressListener progressListener)
            throws Exception {
// The file is something like : /mnt/sdcard/wallpapers/image.jpg
        File mediaFile = new File(file);
        HttpTransport transport = new NetHttpTransport();       

        HttpRequestFactory factory = transport.createRequestFactory(null);
//obtain the upload url
        GenericUrl getUploadUrl = new GenericUrl(Util.getBaseUrl(context)
                + "/myapp/blobservice?get-key");
        HttpRequest request = factory.buildGetRequest(getUploadUrl);
        HttpResponse response1 = request.execute();

        String postUrl = convertStreamToString(response1.getContent()).trim();
// the returned post url when using the development server is something like : //http://192.168.1.4:8888/_ah/upload/agd5YWRnZXQychwLEhVfX0Jsb2JVcGxvYWRTZXNzaW9uX18Y2wUM

        InputStreamContent mediaContent = new InputStreamContent("image/jpeg",
                new BufferedInputStream(new FileInputStream(mediaFile)));
        mediaContent.setLength(mediaFile.length());
        MediaHttpUploader uploader = new MediaHttpUploader(mediaContent,transport, null);

        uploader.setProgressListener(progressListener);

//the following line produces exception "500 Java heap space" on the local server
//on the remote server I get "500: Internal server error"

        HttpResponse response = uploader.upload(new GenericUrl(postUrl));
        if (!response.isSuccessStatusCode()) {
            throw new Exception("Uploading image failed.");
        } else {
            String blobKey = convertStreamToString(response.getContent()).trim();
            return blobKey;
        }
    }

1 个答案:

答案 0 :(得分:0)

似乎MediaHttpUpload使用的resumable protocolmultipart/form-data file upload of GAE blobstore不兼容。

您应该创建一个知道如何处理custom data upload servletresumable protocol