0
            MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<String, Object>();
            FileSystemResource fileSystemResource = new FileSystemResource(file.getAbsoluteFile()) {

                @Override
                public String getFilename() {
                    String filename = fFilename;

                    if (fContentType.toLowerCase().equals("image/jpeg")) {
                        if (!filename.toLowerCase().endsWith(".jpeg")) {
                            filename += ".jpeg";
                        }
                    }

                    return filename;
                }
            };

            MediaType mediaType = MediaType.valueOf(contentType);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(mediaType);
            HttpEntity httpEntity = new HttpEntity(fileSystemResource, headers);

            multiValueMap.add("file", httpEntity);

            additionalHeaders.put("Accept", "");
            mailboxServiceHttpAuthStore.setHeaders(additionalHeaders);
            // postAttachment is generated by androidannoations, outOfMemory Exception starts here
            jsonAttachmentCreated = mailboxServiceRestClient.postAttachment(url, multiValueMap);

When transfering a larger pdf file like ~15-20Mb it causes an OutOfMemory Exception

E/dalvikvm-heapīš• Out of memory on a 33546614-byte allocation.

Is there any why to prevent this? Or to buffer the pdf file?

true-mt
  • 347
  • 3
  • 12

1 Answers1

0

I didn't test it, but I think you could resolve this by using ResponseExtractor as suggested by Louis's solution.

To do so, you have to call restTemplate like this :

File file = (File) restTemplate.execute(rootUrl.concat("/mywebservice"), HttpMethod.GET, requestCallabck, responseExtractor, uriVariables);

With response extractor implemented with the following code :

public class FileResponseExtractor implements ResponseExtractor<File> {
            ...

    public void setListener(ReceivingListener listener) {
        this.listener = listener;
    }

    @Override
    public File extractData(ClientHttpResponse response) throws IOException {
        InputStream is = response.getBody();

        long contentLength = response.getHeaders().getContentLength();

        long availableSpace = AvailableSpaceHandler.getExternalAvailableSpaceInMB();
        long availableBytes = AvailableSpaceHandler.getExternalAvailableSpaceInBytes();
        Log.d(TAG, "available space: " + availableSpace + " MB");

        long spareSize = 1024 * 1024 * 100;
        if(availableBytes < contentLength + spareSize) {
            throw new NotEnoughWritableMemoryException(availableSpace);
        }

        File f = new File(temporaryFileName);
        if (f.exists())
            f.delete();
        f.createNewFile();

        OutputStream o = new FileOutputStream(f);

        listener.onStart(contentLength, null);
        boolean cancel = false;
        try {
            byte buf[] = new byte[bufferSize];
            int len;
            long sum = 0;
            while ((len = is.read(buf)) > 0) {
                o.write(buf, 0, len);
                sum += len;
                listener.onReceiving(sum, len, null);

                cancel = !listener.onContinue();
                if(cancel) {
                    Log.d(TAG, "Cancelled!!!");
                    throw new CancellationException();
                }
            }
        } finally {
            o.close();
            is.close();
            listener.onFinish(null);

            if(cancel) {
                f.delete();
            }
        }

        return f;

    }

}

But, AndroidAnnotations generates RestClient code by using restTemplate.exchange(...) method. So you'll have to add the "magic" method RestTemplate getRestTemplate() in your @Rest annotated interface in order to retrieve the instance of RestTemplate and call the right method described before.

Hope it helps.

Community
  • 1
  • 1
DayS
  • 1,561
  • 11
  • 15