OutOfMemoryError loading Bitmap via DefaultHttpClient

Posted by Goddchen on Stack Overflow See other posts from Stack Overflow or by Goddchen
Published on 2012-11-02T22:38:38Z Indexed on 2012/11/02 23:00 UTC
Read the original article Hit count: 145

Filed under:

i have a simple problem: Although i'm using sampleSize properly, my code doesn't even reach the BitmapFactorycode, since DefaultHttpClient is already throwing the exception.

Here is my code:

                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet(mSongInfo.imageLarge);
                HttpResponse response = client.execute(request);
                int sampleSize = 1;
                while (response.getEntity().getContentLength() / sampleSize
                        / sampleSize > 100 * 1024) {
                    sampleSize *= 2;
                }
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = sampleSize;
                final Bitmap bitmap = BitmapFactory.decodeStream(response
                        .getEntity().getContent(), null, options);

And here is the exception:

0   java.lang.OutOfMemoryError: (Heap Size=11463KB, Allocated=7623KB, Bitmap Size=9382KB)
1       at org.apache.http.util.ByteArrayBuffer.<init>(ByteArrayBuffer.java:53)
2       at org.apache.http.impl.io.AbstractSessionInputBuffer.init(AbstractSessionInputBuffer.java:82)
3       at org.apache.http.impl.io.SocketInputBuffer.<init>(SocketInputBuffer.java:98)
4       at org.apache.http.impl.SocketHttpClientConnection.createSessionInputBuffer(SocketHttpClientConnection.java:83)
5       at org.apache.http.impl.conn.DefaultClientConnection.createSessionInputBuffer(DefaultClientConnection.java:170)
6       at org.apache.http.impl.SocketHttpClientConnection.bind(SocketHttpClientConnection.java:106)
7       at org.apache.http.impl.conn.DefaultClientConnection.openCompleted(DefaultClientConnection.java:129)
8       at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:173)
9       at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10      at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11      at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
12      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
13      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
14      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
15      at de.goddchen.android.easysongfinder.fragments.SongFragment$1.run(SongFragment.java:79)
16      at java.lang.Thread.run(Thread.java:1027)

As you can see, the code doesn't even reach the part where i check the size (Content-Length) of the image and calculate a proper sample size.

I wasn't aware that simply calling DefaultHttpClient.execute(...) will already load the complete content into the memory.

Am i doing something wrong? What is the right way to first retrieve the content length and then start reading the content from an InputStream?

EDIT To avoid common answers that show how to load images from a URL: i already know how to do that, i have also posted the code above, so why do you keep referencing tutorials on that? I explicitly was very clear about the problem: Why is HttpClient.execute(...)already fetching the whole content and storing it in memory instead of providing a proper ÌnputStreamto me? Please don't post any beginner tutorials on how to load aBitmap`from a URL...

© Stack Overflow or respective owner

Related posts about android