Thursday, 27 January 2011

Android - OutOfMemoryError: bitmap size exceeds VM budget

When you see this error in your console, you're most certainly using some large bitmaps in your application which are taking up a of memory (especially T-Mobile G1, limited to 16 MB).

The solution to this problem is to create a temp storage and pass it to BitmapFactory before decoding. Take into consideration that this error isn't necessarily triggered when handling bitmaps programmatically, but also when specifying them through xml.

If you do specify the bitmaps through xml, for example a background image for a RelativeLayout, just remove the background declaration from xml, and declare the background programmatically, after properly decoding the bitmap.

So, here's the code:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];
Bitmap bitmapImage = BitmapFactory.decodeResource(getResources(), R.drawable.background, options);

If you want to set this bitmap as background, just create a BitmapDrawable, and set it as background:

BitmapDrawable bitmapImageDrawable = new BitmapDrawable(bitmapImage);
RelativeLayout yourRelLayout = (RelativeLayout) findViewById(;

Also, you might want to tile you image when setting it as background, so you'll need to do this before calling setBackgroundDrawable


Don't forget to call bitmapImage.recycle() after usage.


  1. Here is my code to set image from camera but i am not getting image on my imageview.....need help ...

    Bitmap photo = (Bitmap)data.getExtras().get("data");

    BitmapDrawable bitmapImageDrawable = new BitmapDrawable(photo);

  2. Hey pravin, this is a common issue with the camera. Isn't data.getExtras().get("data") null?
    The Android camera gave me a lot of headaches. I'll post a solution soon to help you with that.

  3. imageUri = Uri.fromFile(sdImageMainDirectory);
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(cameraIntent, IMAGE_CAP);

    This is my code open camera on click. and i am saving image in sdcard using URI. but using URI i am not getting image on my imageview.
    sdImageMainDirectory(its a File object)
    IMAGE_CAP(its integer).

    --- And in my onActivityResult see i am using code below to set image on imageView.---

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == IMAGE_CAP) {
    try {

    Bitmap photo = (Bitmap) data.getExtras().get("data");


    so please help...

    1. Hey, sorry for the late answer. Did you manage to wrok this out?
      If not, try to see if "data.getExtras().get("data");" returns null or not. If it does return null, which is pretty common, you'll have to take a different approapch