我有一个从 SD 加载大位图并旋转它的函数。
然而,在第二次或第三次旋转后,我收到位图大小超出 VM 预算错误。
有什么想法吗?我确实回收了旧位图,不是吗?
public void next(String s, int d)
{
if ( mBitmap!=null ) { mBitmap.recycle(); }
deg = deg + d;
mBitmap = BitmapFactory.decodeFile(s);
Matrix matrix = new Matrix();
matrix.postRotate(deg);
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth() , mBitmap.getHeight(), matrix, true);
谢谢!
最佳答案
当您没有正确使用位图时,内存不足错误并不罕见。
Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices.
有一个很棒的 Android 指南 how to use Bitmaps efficiently.
按照该指南,您应该能够大幅减少内存消耗,而不会损失任何可见的质量,从而避免不必要的崩溃。
关于android - 如何回收位图? - 位图大小超出 VM 预算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12125428/