-3

I wrote this code that's supposed to load a bitmap from assets or from sd card. Usually the bitmap is no too big, <1mb , 1280x800 , but sometimes it throws me an OutOfMemoryError: bitmap size exceeds VM budget error, yet when I try to reload it again, most times it load fine.

Any ideas what might be wrong here?

Thanks! :)

                try
    {

    GirlBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.girl);
    GirlBitmapBG = BitmapFactory.decodeResource(getResources(), R.drawable.bg);


                if ( ForeNameSource.equals("ass") )
                {
                    try {
                        InputStream ims = getAssets().open( "girls" +"/"+ ForeName );
                        GirlBitmap = BitmapFactory.decodeStream(ims);
                    } catch (IOException e) { e.printStackTrace(); }
                }
                else
                {
                    GirlBitmap = BitmapFactory.decodeFile(ForeName);
                }


                if ( BgNameSource.equals("ass") )
                {
                    try {
                        InputStream imsBg = getAssets().open( "girls" +"/"+ BgName );
                        GirlBitmapBG = BitmapFactory.decodeStream(imsBg);
                    } catch (IOException e) { e.printStackTrace(); }
                }
                else
                {
                    GirlBitmapBG = BitmapFactory.decodeFile(BgName);
                }
    }
    catch(OutOfMemoryError e){
      Log.e("out of memory","too big!");  /// sometimes crashes here.
      Toast.makeText(this, "Bitmap too big!", 1500).show();
    }


    if ( ReSize )
    {
    WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();


    float ReSizeCoofGirl =  (float)display.getHeight() / (float)GirlBitmap.getHeight();
    if ( ReSizeCoofGirl > 1 ) { ReSizeCoofGirl = 1; }

    float ReSizeCoofBG =  (float)display.getHeight() / (float)GirlBitmapBG.getHeight();
    if ( ReSizeCoofBG > 1 ) { ReSizeCoofBG = 1; }

    // also sometimes crash comes from the line below.
    GirlBitmap = Bitmap.createScaledBitmap(GirlBitmap, (int) (GirlBitmap.getWidth()*ReSizeCoofGirl), (int) (GirlBitmap.getHeight()*ReSizeCoofGirl), false);
    GirlBitmapBG = Bitmap.createScaledBitmap(GirlBitmapBG, (int) (GirlBitmapBG.getWidth()*ReSizeCoofBG), (int) (GirlBitmapBG.getHeight()*ReSizeCoofBG), false);
    }

    drawView.invalidate();
}
Roger Travis
  • 8,402
  • 18
  • 67
  • 94
  • 5
    Maybe first try related questions like: http://stackoverflow.com/questions/1586685/outofmemoryerror-bitmap-size-exceeds-vm-budget-android, http://stackoverflow.com/questions/2928002/outofmemoryerror-bitmap-size-exceeds-vm-budget-android, http://stackoverflow.com/questions/3037027/android-outofmemoryerror-bitmap-size-exceeds-vm-budget-with-no-reason-i-can-se, http://stackoverflow.com/questions/3324010/android-outofmemoryerror-bitmap-size-exceeds-vm-budget and http://stackoverflow.com/questions/4990761/android-java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget-android-whil and so on... – Boris Strandjev Apr 26 '12 at 07:27
  • 2
    And: http://stackoverflow.com/questions/5206784/tiny-images-no-rotation-but-still-get-outofmemoryerror-bitmap-size-exceeds-vm, http://stackoverflow.com/questions/5235191/android-bitmap-size-exceeds-vm-budget, http://stackoverflow.com/questions/5584534/android-java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget, http://stackoverflow.com/questions/6131927/bitmap-size-exceeds-vm-budget-in-android and http://stackoverflow.com/questions/6733224/bitmap-size-exceeds-vm-budget-in-android – Boris Strandjev Apr 26 '12 at 07:29
  • <1mb , 1280x800 ... this bitmap will keep at least 4MB – Blackbelt Apr 26 '12 at 07:29
  • you should resize the image directly after you decode it and `recycle()` the unresized one, then proceed to the next image. – zapl Apr 26 '12 at 07:34
  • see my answer here http://stackoverflow.com/questions/10221258/error-when-load-image-bitmap-size-exceeds-vm-budget/10222020#10222020 – Raghu Nagaraju Apr 26 '12 at 07:37
  • @RaghuNagaraju ah crap! I didn't link to this one... – Boris Strandjev Apr 26 '12 at 07:39

1 Answers1

2

see this new resource added on developer site : http://developer.android.com/training/displaying-bitmaps/index.html (check the example project on right side of the screen)

Iulia Barbu
  • 1,522
  • 12
  • 25