Wednesday, 7 August 2013

Issues with inputstream outofmemory errors

Issues with inputstream outofmemory errors

I'm using the SpringFramework for Android to get my inputstreams.
@Override
public InputStream getImageStream(String url) {
InputStream is = new ByteArrayInputStream(template.getForObject(url,
byte[].class));
return is;
}
For the first few inputstreams its going ok. No Problems at all, but then,
I think it tries to get a very big inputstream. So then I get the
outofmemory error.
I see a lot of posts using something like the 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 this code is to read the inputstream in chunks right?
But the outofmemory error I'm getting is before I can even start the
readBytes. I tried putting resets everywhere...I thought maybe I should
clear the memory somewhere after readbytes or something. But I do not know
how and I don't know if that is the right way?
I think I'm having the basics wrong? I'm very new to android and java...
Is there a way of getting the InputStream another way? I also read
something about BufferedInputStream but I just can't think up of a way to
fit it in.
My goal is to store a blob of the image in the database. And my input is
the imgurl via oauth.
I can also call less quality versions of the inputstream through another
url. And then everything works... But I wanted to try it with the original
image url, because maybe later I want to have the ability to download an
original for printing the photo.

No comments:

Post a Comment