title: Android SurfaceView BitmapFactory out of memory exceptions
body: <p>I have run into a very interesting situation. I have access to two devices and one of them is one of the most top notch androids out there (Galaxy Note) and the other is a decent little HTC Inspire. Now thanks to the difference in these two devices, I was able to notice that the Galaxy Note loads my application fine, but the Inspire returns an outofmemoryexception. And the stack trace suggests that it is the Bitmaps in my code. I have 5 bitmaps that are very high res (like 1280 x 800). And i initialize each of them in the surfaceview's constructor. I know that is a dumb idea, but my Note is able to handle the load. However, crappier devices can't handle all that Bitmap data. How do i solve my problem? I can lower the resolution but thats the last resort. Here is code similar to mine:</p>
  
  <pre><code>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...
  } 
  </code></pre>