2012-02-27 44 views
0

我遇到了一个非常有趣的情况。我可以使用两个设备,其中一个是最出色的机器人之一(Galaxy Note),另一个是体面的HTC Inspire。现在得益于这两款设备的不同,我能够注意到Galaxy Note加载了我的应用程序,但Inspire返回了内存不足的情况。堆栈跟踪表明它是我的代码中的位图。我有5个非常高分辨率的位图(如1280 x 800)。我在Surfaceview的构造函数中初始化它们中的每一个。我知道这是一个愚蠢的想法,但我的笔记能够处理负载。但是,更糟糕的设备无法处理所有的Bitmap数据。我如何解决我的问题?我可以降低分辨率,但这是最后的手段。下面是代码类似地雷:Android SurfaceView BitmapFactory内存不足异常

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... 
} 
+1

你不能,你可以玩的唯一事情就是http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize 你还需要尽快有免费图资源b1.recycle()和b1 = null – sherpya 2012-02-27 06:04:23

+0

我的解决方案是Bitmap.createScaledBitmap(b1,width,height,true);作品完美无瑕。感谢您的输入! – 2012-02-27 06:31:47

回答

1

我的解决办法是Bitmap.createScaledBitmap(B1,宽度,高度,TRUE);