0

Possible Duplicate:
java.lang.OutOfMemoryError: bitmap size exceeds VM budget - Android

the following error occur when loading images to grid view.

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

please help me. is there any solution to this error.

Community
  • 1
  • 1
ibad ur rahman
  • 1,381
  • 4
  • 18
  • 40

1 Answers1

1

try this

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //The new size we want to scale to
            final int REQUIRED_WIDTH=WIDTH;
            final int REQUIRED_HIGHT=HIGHT;
            //Find the correct scale value. It should be the power of 2.
            int scale=1;
            while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
                scale*=2;

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        }
            catch (FileNotFoundException e) {}


        return null;
    }

this will scale bitmap as per width and height you pass..

Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75