OutOfMemory when decoding image from url
<p>I'm getting a lot of these exceptions from users: java.lang.OutOfMemoryError: bitmap size exceeds VM budget - but i'm unable to reproduce the problem on the emulator.</p>

<p>I'm using this as my ImageDownloader <a href="http://code.google.com/p/android-imagedownloader/source/browse/trunk/src/com/example/android/imagedownloader/ImageDownloader.java#185" rel="nofollow">http://code.google.com/p/android-imagedownloader/source/browse/trunk/src/com/example/android/imagedownloader/ImageDownloader.java#185</a> and the problem occurs on line 185 when trying to decode the image. </p>

<p>There is a lot of other questions here on stackoverflow, regarding this error, but none that fits my specific case. The solution in this question could work: <a href="http://stackoverflow.com/a/823966">http://stackoverflow.com/a/823966</a> but i can't seem to get it to work.</p>

<p>Here is what i changed in my ImageDownloader:</p>

<pre><code>final HttpEntity entity = response.getEntity();
if (entity != null) {
    InputStream inputStream = null;
    try {
        inputStream = entity.getContent();
        // return BitmapFactory.decodeStream(inputStream);
        // Bug on slow connections, fixed in future release.

        FlushedInputStream flushedStream = new FlushedInputStream(inputStream);

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(flushedStream, null, o);
        Log.d(LOG_TAG, "decodeStream - outWidth=" + o.outWidth + " outHeight=" + o.outHeight);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 70;

        // Find the correct scale value. It should be the power of
        // 2.
        int scale = 1;
        while (o.outWidth / scale / 2 &gt;= REQUIRED_SIZE &amp;&amp; o.outHeight / scale / 2 &gt;= REQUIRED_SIZE)
            scale *= 2;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(flushedStream, null, o2);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        entity.consumeContent();
    }
}
</code></pre>

<p>It fails when calling decodeStream() the second time. Error is <strong>SkImageDecoder::Factory returned null</strong>. I figured maybe the inputStream is closed or not available on the second call, so i then tried to use BufferedHttpEntity in the first line above, but with no luck.</p>

<p>Can someone guide me towards the right direction? Thanks.</p>
