title: Android Divide image into pieces and get equivalent size of image pieces(chunks)
body: <p>First of all sorry for my bad English,i am working on <strong>Image Splitter</strong> app and it is completed,but now the requirement is that when the image is <strong>split(divide into pieces/chunks)</strong> then the every piece(chunk) of <strong>image block is 50*50 or 40*40</strong>,and the most important thing is that for example the original  image size is 420*320(it is dynamic and i get the image from gallery),then after split(divide) the image into pieces(chunks) the image size will be still the same 420*320 as i mentioned above,and for example the image size is 420*320 and after split the image and divide every block size equivalent 50*50,then the remaining  20 size will be assign to the last or any block,so i have 2 issue:</p>
  
  <p><strong>Note: In my app i am getting the image into gallery then split the image and <code>shuffle the image pieces(chunks)</code>, and then merge the image, and create a **canvas for drawing</strong> all those small(chunks) images .**</p>
  
  <ol>
  <li>after splitting the 420*320 size of image,need the image block(chunk) size 50*50 or 40*40.</li>
  <li>the remaining 20*20 block(chunk) assign to the last block or any of the other block.</li>
  </ol>
  
  <p>This is the original image before splitting and it's dimension is 420*320:</p>
  
  <p><img src="https://i.stack.imgur.com/6cqUJ.jpg" alt="enter image description here"></p>
  
  <p>and this is the image after splitting,the overall image dimension is same 420*320,but the piece(chunk) of image block size is 84*64,<strong>and i want to block size is 50*50 or 40*40</strong> and overall image size will also be same 420*320,and the remaining size will be assign to the last block.</p>
  
  <p><img src="https://i.stack.imgur.com/vaFTr.png" alt="enter image description here"></p>
  
  <p>This is my Activity:</p>
  
  <pre><code>package com.example.imagesplitter;
  
  import java.io.ByteArrayOutputStream;
  import java.util.ArrayList;
  import java.util.Collections;
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.database.Cursor;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.graphics.drawable.BitmapDrawable;
  import android.net.Uri;
  import android.os.Bundle;
  import android.provider.MediaStore;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.Window;
  import android.widget.Button;
  import android.widget.ImageView;
  
  
  //public class ImageActivity extends Activity implements OnClickListener {
  public class ImageActivity extends Activity {
  
      Button split_image;
      Button btnGallery;
  
      ImageView image;
      Uri selectedImage;
      private final int RESULT_LOAD_IMAGE = 1;
      int chunkNumbers = 25;
      ArrayList&lt;Bitmap&gt; chunkedImages;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.main);
  
       image = (ImageView) findViewById(R.id.source_image);
  
  
  
      alertDialogForCameraImage();
  
  }
  
  void pickImageFromGallery() {
  
      Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                                                      
  //      startActivityForResult(pickPhoto , 0); 
      startActivityForResult(pickPhoto, RESULT_LOAD_IMAGE);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
  
      switch(requestCode){
  
          case RESULT_LOAD_IMAGE:                
              if(resultCode==Activity.RESULT_OK) {
                //  takenPictureData = handleResultFromChooser(data);  
  
                   selectedImage = data.getData();
                  String[] filePathColumn = {  MediaStore.Images.Media.DATA };
  
                  Cursor cursor = getContentResolver().query(selectedImage,
                          filePathColumn, null, null, null);
                  cursor.moveToFirst();
  
                  int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                  String picturePath = cursor.getString(columnIndex);
                  cursor.close();
  
              //  ImageView imageView = (ImageView) findViewById(R.id.imgView);
                  image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
  
                  // Function of split the image(divide the image into pieces)
                  splitImage(image, chunkNumbers);
              }
              break;          
      }
  
      //And show the result in the image view when take picture from camera.
  
  }
  
  
  public void alertDialogForCameraImage() {
      AlertDialog.Builder adb = new AlertDialog.Builder(ImageActivity.this);
      adb.setTitle("Pick Image From Gallery: ");
      adb.setNegativeButton("Gallery", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
  
              pickImageFromGallery();
  
        } });
      adb.show();
  }
  
  
  
  /**
   * Splits the source image and show them all into a grid in a new activity
   * 
   * @param image The source image to split
   * @param chunkNumbers The target number of small image chunks to be formed from the   source image
   */
  private void splitImage(ImageView image, int chunkNumbers) {    
  
      //For the number of rows and columns of the grid to be displayed
      int rows,cols;
  
      //For height and width of the small image chunks 
      int chunkHeight,chunkWidth;
  
      //To store all the small image chunks in bitmap format in this list 
       chunkedImages = new ArrayList&lt;Bitmap&gt;(chunkNumbers);
  
      //Getting the scaled bitmap of the source image
      BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
      Bitmap bitmap = drawable.getBitmap();
      /*ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);*/
      Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
  
      rows = cols = (int) Math.sqrt(chunkNumbers);
      chunkHeight = bitmap.getHeight()/rows;
      chunkWidth = bitmap.getWidth()/cols;
      /*chunkHeight = 300/rows;
      chunkWidth = 300/cols;*/
  
      //xCoord and yCoord are the pixel positions of the image chunks
      int yCoord = 0;
      for(int x=0; x&lt;rows; x++){
          int xCoord = 0;
          for(int y=0; y&lt;cols; y++){
              chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
              xCoord += chunkWidth;
          }
          yCoord += chunkHeight;
      }
  
      // Function of merge the chunks images(after image divided in pieces then i can call this function to combine and merge the image as one)
      mergeImage(chunkedImages);
  
  
  }
  
   void mergeImage(ArrayList&lt;Bitmap&gt; imageChunks) {
  
      Collections.shuffle(imageChunks);
  
      //Get the width and height of the smaller chunks
      int chunkWidth = imageChunks.get(0).getWidth();
      int chunkHeight = imageChunks.get(0).getHeight();
  
      //create a bitmap of a size which can hold the complete image after merging
      Bitmap bitmap = Bitmap.createBitmap(chunkWidth * 5, chunkHeight * 5,  Bitmap.Config.ARGB_4444);
  
      //create a canvas for drawing all those small images
      Canvas canvas = new Canvas(bitmap);
      int count = 0;
      for(int rows = 0; rows &lt; 5; rows++){
          for(int cols = 0; cols &lt; 5; cols++){
              canvas.drawBitmap(imageChunks.get(count), chunkWidth * cols, chunkHeight * rows, null);
              count++;
          }
      }
  
      /*
       * The result image is shown in a new Activity
       */
  
      Intent intent = new Intent(ImageActivity.this, MergedImage.class);
      intent.putExtra("merged_image", bitmap);
      startActivity(intent);
      finish();
  
  }
  }
  </code></pre>
  
  <p>and this is my image split method:</p>
  
  <pre><code>private void splitImage(ImageView image, int chunkNumbers) {    
  
      //For the number of rows and columns of the grid to be displayed
      int rows,cols;
  
      //For height and width of the small image chunks 
      int chunkHeight,chunkWidth;
  
      //To store all the small image chunks in bitmap format in this list 
       chunkedImages = new ArrayList&lt;Bitmap&gt;(chunkNumbers);
  
      //Getting the scaled bitmap of the source image
      BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
      Bitmap bitmap = drawable.getBitmap();
      /*ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);*/
      Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
  
      rows = cols = (int) Math.sqrt(chunkNumbers);
      chunkHeight = bitmap.getHeight()/rows;
      chunkWidth = bitmap.getWidth()/cols;
      /*chunkHeight = 300/rows;
      chunkWidth = 300/cols;*/
  
      //xCoord and yCoord are the pixel positions of the image chunks
      int yCoord = 0;
      for(int x=0; x&lt;rows; x++){
          int xCoord = 0;
          for(int y=0; y&lt;cols; y++){
              chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
              xCoord += chunkWidth;
          }
          yCoord += chunkHeight;
      }
  
  
      mergeImage(chunkedImages);
  
  }
  </code></pre>
  
  <p>Any help will be highly appreciated Thanks alot in advance.</p>
  
  <p><strong>EDITED:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/U8uBi.png" alt="enter image description here"></p>
  
  <p><strong>Updated:</strong>
  This is the example image and i want it like this:</p>
  
  <p><img src="https://i.stack.imgur.com/z5RqK.png" alt="enter image description here"></p>
  
  <p><strong>Updated:</strong>
  i think it should be like this:</p>
  
  <p><img src="https://i.stack.imgur.com/DlJ9f.png" alt="enter image description here"></p>