Android, image loading and memory allocations
I have an app that contains several custom views that extend View. Each custom view loads up a bunch of images (~20) and scales them down:
mBmp[img1] = BitmapFactory.decodeStream(context.getAssets().open("img01.png"));
mBmp[img2] = BitmapFactory.decodeStream(context.getAssets().open("img02.png"));
mBmp[img3] = BitmapFactory.decodeStream(context.getAssets().open("img03.png"));
...
mBmp[img20] = BitmapFactory.decodeStream(context.getAssets().open("img20.png"));
This is all within a try - catch block.
This part does not cause any problem. However, once I reach the point of scaling the images and I call my scale method which uses createScaledBitmap:
protected void scaleImages(float mWidth, float mHeight)
{
float mXRat = mWidth/1024;
float mYRat = mHeight/600;
for (int i = 0; i < mBmp.length; i++)
{
float oldX = mBmp[i].getWidth();
float oldY = mBmp[i].getHeight();
double newX = (mXRat * oldX);
double newY = (mYRat * oldY);
Bitmap newBmp = Bitmap.createScaledBitmap(mBmp[i], (int) newX, (int) newY, true);
mBmp[i] = newBmp;
}
}
I get an error that I am trying to allocate too much memory.
01-25 10:16:44.227: ERROR/dalvikvm-heap(14548): 1497600-byte external allocation too large for this process.
01-25 10:16:44.227: ERROR/dalvikvm(14548)开发者_运维问答: Out of memory: Heap Size=4871KB, Allocated=2544KB, Bitmap Size=18683KB
01-25 10:16:44.227: ERROR/GraphicsJNI(14548): VM won't let us allocate 1497600 bytes
No single image of mine should take this kind of memory when expanded into RAM, so why is it running out when trying to resize them?
this is not single imagesize .this message is shown bcoz u r decoding it into bitmap and it is stored in heap for temp.so it is displaying tht msg.
精彩评论