title: Bitmap out of bounds
body: <p>i'm trying to create a function that croppes images in 3x3/4x4/5x5 depending on the difficulty a player chooses, and puts them in a bitmap array. But when i run this function i get the error:</p>
  
  <p>x + width  must be &lt;= bitmap.width()!</p>
  
  <p>I tried to see what i was doing wrong but i can't see it. Here is the code:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Bundle extras = this.getIntent().getExtras();
      resource = extras.getInt("Image");
      difficulty = extras.getInt("difficulty");
      Bitmap bmp = BitmapFactory.decodeResource(getResources(), resource);
      divideImages(bmp);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>private void divideImages(Bitmap bmp) {
  
      int width = bmp.getWidth();
      int height =bmp.getHeight();
      float w_ratio = (float) width/bmp.getWidth();
      int scaledwidth = (int) (bmp.getWidth()*w_ratio);
      int scaledheight = (int) (bmp.getHeight()*w_ratio);
      Bitmap scaledbmp = Bitmap.createScaledBitmap(bmp,scaledwidth,scaledheight,true);
  
      switch(difficulty){
          case 0:
              for(int i=0;i&lt;3;i++){
  
                  System.out.println("row no. "+i);
  
                  for(int j=0;j&lt;3;j++){
  
                      System.out.println("Column no."+j);
                      int startx = (scaledwidth/3)*j;
                      int starty = (scaledheight/3)*i;
                      Bitmap b1  = Bitmap.createBitmap(scaledbmp,startx,starty,scaledwidth,scaledheight);
                      bs.add(b1);
                      b1 = null;
                      }
                  bmp = null;
              }
              break;                                
          case 1:
              for(int i=0;i&lt;4;i++){
  
                  System.out.println("row no. "+i);
  
                  for(int j=0;j&lt;4;j++){
  
                      System.out.println("Column no."+j);
                      int startx = (scaledwidth/4)*j;
                      int starty = (scaledheight/4)*i;
                      Bitmap b1  = Bitmap.createBitmap(bmp,startx,starty,scaledwidth,scaledheight);
                      bs.add(b1);
                      b1 = null;
                      }
                  bmp = null;
              }
          case 2:
              for(int i=0;i&lt;5;i++){
  
                  System.out.println("row no. "+i);
  
                  for(int j=0;j&lt;5;j++){
  
                      System.out.println("Column no."+j);
                      int startx = (scaledwidth/5)*j;
                      int starty = (scaledheight/5)*i;
                      Bitmap b1  = Bitmap.createBitmap(bmp,startx,starty,scaledwidth,scaledheight);
                      bs.add(b1);
                      b1 = null;
                      }
                  bmp = null;
              }
      }
  }
  </code></pre>
  
  <p>I suppose Bitmap b1 is out of boundary but i don't seem to see why.....</p>