http - Android: Androidannotations Rest post with pdf causes OutOfMemory Exception -
http - Android: Androidannotations Rest post with pdf causes OutOfMemory Exception -
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"; } } homecoming 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 generated androidannoations, outofmemory exception starts here jsonattachmentcreated = mailboxservicerestclient.postattachment(url, multivaluemap);
when transfering larger pdf file ~15-20mb causes outofmemory exception
e/dalvikvm-heap﹕ out of memory on 33546614-byte allocation.
is there why prevent this? or buffer pdf file?
i didn't test it, think resolve using responseextractor
suggested louis's solution.
to so, have phone call resttemplate :
file file = (file) resttemplate.execute(rooturl.concat("/mywebservice"), httpmethod.get, requestcallabck, responseextractor, urivariables);
with response extractor implemented next code :
public class fileresponseextractor implements responseextractor<file> { ... public void setlistener(receivinglistener listener) { this.listener = listener; } @override public file extractdata(clienthttpresponse response) throws ioexception { inputstream = 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; seek { 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(); } } } { o.close(); is.close(); listener.onfinish(null); if(cancel) { f.delete(); } } homecoming f; } }
but, androidannotations generates restclient code using resttemplate.exchange(...)
method. you'll have add together "magic" method resttemplate getresttemplate()
in @rest
annotated interface in order retrieve instance of resttemplate
, phone call right method described before.
hope helps.
android http post out-of-memory android-annotations
Comments
Post a Comment