1

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.

Stacktrace:

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

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;
    }
}       
naresh
  • 9,682
  • 25
  • 77
  • 123
  • decrease the size of the images and try – Abhi Sep 12 '11 at 08:55
  • here i am compressing the image it is not sufficient then how to decrease the size? – naresh Sep 12 '11 at 09:05
  • You seem to decompress them in memory and then recompress them in save... maybe skip that and save the stream as-is. – Torp Sep 12 '11 at 09:13
  • 1
    Check this Question:: http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue http://stackoverflow.com/questions/5082703/android-out-of-memory-error-with-lazy-load-images http://stackoverflow.com/questions/1949066/java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget-android http://stackoverflow.com/questions/3037027/android-outofmemoryerror-bitmap-size-exceeds-vm-budget-with-no-reason-i-can-see – Samir Mangroliya Sep 12 '11 at 09:27
  • Any luck resolving this issue? I'm seeing something similar... – Alan Moore Jan 21 '12 at 04:16
  • This is very common problem and has been discussed many times. You can start from http://developer.android.com/intl/ru/training/displaying-bitmaps/load-bitmap.html to understand a problem. Also check links that @SamirMangroliya has provided. – Viacheslav Sep 10 '13 at 06:12

1 Answers1

0

I think you are not using the ViewHolder model for listview. In such case you are supposed the get such error as all the list items will be available in the memory whether it is visible or not.By using ViewHolder memory is assigned to only those items which are visible and are currently on the screen. ViewHolder patternincreses the performance by 150% For video tutorial about ViewHolder check out this .If you are already implemented then it is the case of memory leak you can refer this

Shakeeb Ayaz
  • 5,919
  • 5
  • 37
  • 62