【问题标题】:Android SurfaceView BitmapFactory out of memory exceptionsAndroid SurfaceView BitmapFactory 内存不足异常
【发布时间】:2012-02-27 06:00:46
【问题描述】:

我遇到了一个非常有趣的情况。我可以使用两台设备,其中一台是目前最顶尖的机器人之一(Galaxy Note),另一台是体面的小型 HTC Inspire。现在,由于这两种设备的不同,我能够注意到 Galaxy Note 可以正常加载我的应用程序,但 Inspire 返回内存不足异常。堆栈跟踪表明它是我代码中的位图。我有 5 个非常高分辨率的位图(例如 1280 x 800)。我在surfaceview的构造函数中初始化它们中的每一个。我知道这是一个愚蠢的想法,但我的 Note 能够处理负载。然而,更糟糕的设备无法处理所有的位图数据。我该如何解决我的问题?我可以降低分辨率,但这是最后的手段。这是类似于我的代码:

static public class GraphicsView extends SurfaceView implements Runnable {

    Thread t;
    SurfaceHolder holder;
    boolean sentinel = false;
    Bitmap b1, b2, b3, b4, b5;

    public GraphicsView(Context context) {
        super(context);
        holder = getHolder();
        b1 = BitmapFactory.decodeResource(getResources(), R.drawable.i);
        b2 = BitmapFactory.decodeResource(getResources(), R.drawable.like);
        b3 = BitmapFactory.decodeResource(getResources(), R.drawable.csharp);
        b4 = BitmapFactory.decodeResource(getResources(), R.drawable.python_and);
        b5 = BitmapFactory.decodeResource(getResources(), R.drawable.java);
        /**
        *
        * This is where the exceptions come!
        *
        **/
    }       

    public void run() {
        while (sentinel) {
            if(!holder.getSurface().isValid()) {
                continue;
            }
            try {
                Canvas canvas = holder.lockCanvas();
                //Drawing commands go here...ommited. These commands may use any of the 5 bitmaps                   
                holder.unlockCanvasAndPost(canvas);
            }catch(Exception e) {
                //Excpetion handling...its not getting here anyways
            }
        }
    }
    //More code below...Ommited...
} 

【问题讨论】:

  • 你不能,你唯一可以玩的是developer.android.com/reference/android/graphics/…你还需要尽快释放位图资源 b1.recycle() 和 b1 = null
  • 我的解决方案是 Bitmap.createScaledBitmap(b1, width, height, true);完美无瑕。感谢您的意见!

标签: java android bitmap out-of-memory surfaceview


【解决方案1】:

我的解决方案是 Bitmap.createScaledBitmap(b1, width, height, true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-24
    • 2023-03-06
    • 1970-01-01
    • 2014-10-21
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    相关资源
    最近更新 更多