android - 如何回收位图? - 位图大小超出 VM 预算

标签 android

我有一个从 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/

相关文章:

android - 不能将 PowerMock 与 Robolectric 一起使用

android - 从 Android 获取崩溃堆栈跟踪的最简单方法是什么?

java - getActivity() 或 getContext() 用于带有 viewpager 的 fragment ?

android - 进度条显示背景

javascript - 如何从App访问动态生成的网页内容?

java - Jackson 反序列化 json Map - Map<String,Map<String,String>> 作为一个对象

java - 如何在android中导入特定版本的 Fabric

android - 将 PreferenceFragment 与多个首选项文件重用

java - 无法构建新的 Openfire 插件

android - BackUpAgentHelperClass 没有被调用