0

I am developing an Android Application to load some images to emulator, but it fails with the below error:

11-07 19:38:51.596: E/dalvikvm-heap(322): 73670400-byte external
allocation too large for this process.

11-07 19:38:51.596: E/GraphicsJNI(322): VM won't let us allocate
73670400 bytes

11-07 19:38:51.596: D/AndroidRuntime(322): Shutting down VM

11-07 19:38:51.616: W/dalvikvm(322): threadid=1: thread exiting with
uncaught exception (group=0x4001d800)

11-07 19:38:51.926: E/AndroidRuntime(322): FATAL EXCEPTION: main

11-07 19:38:51.926: E/AndroidRuntime(322): java.lang.OutOfMemoryError:
bitmap size exceeds VM budget

11-07 19:38:51.926: E/AndroidRuntime(322):  at
android.graphics.Bitmap.nativeCreate(Native Method)

11-07 19:38:51.926: E/AndroidRuntime(322):  at
android.graphics.Bitmap.createBitmap(Bitmap.java:468)

11-07 19:38:51.926: E/AndroidRuntime(322):  at
android.graphics.Bitmap.createBitmap(Bitmap.java:435)

My Java code is below in which the error is occurring. I used System.out.println to debug the issue.

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    System.out.println("11");
    ImageView imageView = new ImageView(context);
    System.out.println("12");


    imageView.setImageResource(imageIDs[position]);
    System.out.println("13");
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    System.out.println("14");
    imageView.setLayoutParams(new Gallery.LayoutParams(150, 220));
    System.out.println("15");
    imageView.setBackgroundResource(itemBackGround);

    System.out.println("16");

    return imageView;
}

If you can help, I would really appreciate that. Thanks.

Kara
  • 6,115
  • 16
  • 50
  • 57
selami
  • 21
  • 2

2 Answers2

1

Try catching the expection and retrying after calling a garbage collection

public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        System.out.println("11");
        ImageView imageView = new ImageView(context);
        System.out.println("12");
        try {

            imageView.setImageResource(imageIDs[position]);
            System.out.println("13");
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            System.out.println("14");
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 220));
            System.out.println("15");
            imageView.setBackgroundResource(itemBackGround);


    } catch (OutOfMemoryError e) {
            // run your image cache clean up here if you have one
            // call sys gc
    System.gc();        

            imageView.setImageResource(imageIDs[position]);
            System.out.println("13");
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            System.out.println("14");
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 220));
            System.out.println("15");
            imageView.setBackgroundResource(itemBackGround);
    }

        System.out.println("16");

        return imageView;
}
petey
  • 16,914
  • 6
  • 65
  • 97
  • 11-07 20:35:42.905: I/System.out(350): 12 11-07 20:35:44.449: E/dalvikvm-heap(350): 73670400-byte external allocation too large for this process. 11-07 20:35:44.449: E/GraphicsJNI(350): VM won't let us allocate 73670400 bytes 11-07 20:35:45.105: D/dalvikvm(350): GC_EXPLICIT freed 851 objects / 60568 bytes in 628ms 11-07 20:35:45.118: I/System.out(350): 13 11-07 20:35:45.118: I/System.out(350): 14 11-07 20:35:45.118: I/System.out(350): 15 11-07 20:35:45.315: I/System.out(350): 16 – selami Nov 07 '12 at 20:41
  • 11-07 20:35:45.905: E/dalvikvm-heap(350): 73670400-byte external allocation too large for this process. 11-07 20:35:45.905: E/GraphicsJNI(350): VM won't let us allocate 73670400 bytes 11-07 20:35:45.925: D/AndroidRuntime(350): Shutting down VM 11-07 20:35:45.925: W/dalvikvm(350): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 11-07 20:35:46.515: E/AndroidRuntime(350): FATAL EXCEPTION: main 11-07 20:35:46.515: E/AndroidRuntime(350): java.lang.OutOfMemoryError: bitmap size exceeds VM budget – selami Nov 07 '12 at 20:41
  • oh. Your image is WAY too big to display, you will need to resize it, here is another question answer that will do this for you : http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – petey Nov 07 '12 at 21:52
0

Apparently the image file is 70 megabytes, that would be your problem. This is much larger than an android applications allowable heap size.

Paul Harris
  • 5,769
  • 1
  • 25
  • 41