android - Out of memory problem
<p>In my app, i am downloading images from the web. For this, first time i am downloading images from the web and these images are stored in the sdcard. Next time, i am checking these images are in sdcard or not. if yes, fetching from the sdcard otherwise i am downloading from the web. These images are displayed like list. i am repeatedly(means move up/down continuously) scrolling the list then my app crashed and i am getting the out of memory exception. How to handle it. can anybody help me. </p>

<p>Stacktrace:</p>

<pre><code>09-12 13:40:42.640: ERROR/AndroidRuntime(3426): java.lang.OutOfMemoryError: bitmap size exceeds VM  budget(Heap Size=7879KB, Allocated=3362KB, Bitmap Size=11402KB)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  android.graphics.BitmapFactory.nativeDecodeFile(Native Method)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:355)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:433)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  com.ibkr.elgifto.GiftCategories$itemlistadapter$4.getDrawableFromUrl(GiftCategories.java:830)
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):     at  com.ibkr.elgifto.GiftCategories$itemlistadapter$4.run(GiftCategories.java:739)
09-12 13:53:32.450: INFO/WSP(332): [Receiver] next auto-sync alarm event is 180 mins later, at time: Mon Sep 12 16:53:32 GMT+05:30 2011
</code></pre>

<p>code</p>

<pre><code>private Drawable getDrawableFromUrl(String imageUrl) {

    // TODO Auto-generated method stub
    String filename = imageUrl;
    filename = filename.replace("/", "+");
    filename = filename.replace(":", "+");
    filename = filename.replace("~", "s");
    File elgiftoimagesref = new File("/sdcard/Elgiftoimages/");

    elgiftoimagesref.mkdir();

    final File file = new File(elgiftoimagesref, filename);
    BitmapDrawable SDdrawable = null;

    boolean exists = file.exists();
    if (!exists) {
        try {
            URL myFileUrl = new URL(imageUrl);
            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            final Bitmap result = BitmapFactory.decodeStream(is);
            is.close();
            new Thread() {
                public void run() {
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    if (result != null) {
                        result.compress(Bitmap.CompressFormat.JPEG, 40,
                                bytes);
                    }
                    try {

                        FileOutputStream fo;
                        fo = new FileOutputStream(file);
                        fo.write(bytes.toByteArray());

                        fo.flush();
                        fo.close();
                        // result.recycle();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
            BitmapDrawable returnResult = new BitmapDrawable(result);
            return returnResult;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    } else {

        // Here i am getting the out of memory error.
        SDdrawable = new BitmapDrawable(BitmapFactory.decodeFile(file
                .toString()));
        return SDdrawable;
    }
}       
</code></pre>
