android - Issues with inputstream outofmemory errors -


i'm using springframework android inputstreams.

    @override     public inputstream getimagestream(string url) {      inputstream = new bytearrayinputstream(template.getforobject(url, byte[].class));      return is;  } 

for first few inputstreams going ok. no problems @ all, then, think tries big inputstream. outofmemory error.

i see lot of posts using following code:

public byte[] readbytes(inputstream inputstream) throws ioexception {        bytearrayoutputstream bytebuffer = new bytearrayoutputstream();        int buffersize = 1024;       byte[] buffer = new byte[buffersize];        int len = 0;       while ((len = inputstream.read(buffer)) != -1) {         bytebuffer.write(buffer, 0, len);       }        byte[] bytearray= bytebuffer.tobytearray();        return bytearray; } 

the idea of code read inputstream in chunks right?

but outofmemory error i'm getting before can start readbytes method. tried putting resets everywhere...i thought maybe should clear memory somewhere after readbytes or something. not know how , don't know if right way?

i think i'm having basics wrong? i'm new android , java... there way of getting inputstream way? read bufferedinputstream can't think of way fit in.

my goal store blob of image in database. , input imgurl via oauth.

i can call less quality versions of inputstream through url. , works... wanted try original image url, because maybe later want have ability download original printing photo.

you getting outofmemory error because reading whole data in memory when using bytearrayinputstream. notice when write in bytearrayoutputstream keeps data in memory (it simple wrapper byte array). should use fileoutputstream instead cache.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -