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();
}