Search code examples
androidmemorybitmapdownload

Bytearray java.lang.OutOfMemoryError while downloading images from urls


I'm downloading some images from the internet and storing them in a bitmap array. I have no problem with small images but with larger images since I'm using a bytearray to store the downloaded stream before converting it to a bitmap I get the java.lang.OutOfMemoryError. You can see the code to my method below (timeout code based on this answer by kuester2000).

public static Bitmap downloadBitmap(String url) /*throws IOException,ClientProtocolException*/{
    Bitmap bitmap = null;
    HttpParams param=new BasicHttpParams();
    
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used. 
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(param, timeoutConnection);
    
 // Set the default socket timeout (SO_TIMEOUT) 
 // in milliseconds which is the timeout for waiting for data.
 int timeoutSocket = 5000;
 HttpConnectionParams.setSoTimeout(param, timeoutSocket);
 
    HttpUriRequest request=new HttpGet(url.toString());
    HttpClient httpClient=new DefaultHttpClient(param);
    try {
            HttpResponse response=httpClient.execute(request);
            
            StatusLine statusLine=response.getStatusLine();
            int statusCode=statusLine.getStatusCode();
            if (statusCode==200) {
                HttpEntity entity=response.getEntity();
                byte[] bytes=EntityUtils.toByteArray(entity);
                bitmap=BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                
                //the line below adjusts the width if I set the height to 100px
                int width=(bitmap.getWidth()*100)/bitmap.getHeight();
                
                 bitmap=Bitmap.createScaledBitmap(BitmapFactory.decodeByteArray(bytes,0,bytes.length), width, 100, false);
                
            }
    }
    catch (ClientProtocolException e) {
        Log.e("ReadFeed", "HTTP Error", e);
        return null;
    }
    catch (IOException e) {
        Log.e("ReadFeed", "Connection Error", e);
        return null;
    }
    catch (java.lang.OutOfMemoryError e) {
        e.printStackTrace();
    }
    finally
    {
    httpClient.getConnectionManager().shutdown();
    }
    System.out.println("resized height="+bitmap.getHeight()+"  resized width="+bitmap.getWidth());
    
    return bitmap;

}

Is there a more efficient way to store my bytestream or a better way to handle my http connection?


Solution

  • What about using directly BitmapFactory.decodeStream(), instead of loading the response in a byte array and then decoding it? That should save some memory.