title: Square bitmap before uploading
body: <p>hello i have the following code for getting image from gallery activity and i want the image to be squared to particular dimension say 400*400 before it is uploaded while maintaining its aspect ratio so that all uploaded images be the same size. any ideas please</p>
  
  <pre><code>@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      try {
          // When an Image is picked
          if (requestCode == RESULT_LOAD_IMG &amp;&amp; resultCode == RESULT_OK &amp;&amp; null != data) {
              Uri selectedImage = data.getData();
              String[] filePathColumn = { MediaStore.Images.Media.DATA };
              assert selectedImage != null;
              Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
              assert cursor != null;
              cursor.moveToFirst();
              int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
              imgPath = cursor.getString(columnIndex);
              cursor.close();
              ImageView imgView = findViewById(R.id.imagebutton);
              int width = imgView.getWidth();
              int height = imgView.getHeight();
              Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(imgPath),width, height, false);
              imgView.setImageBitmap(bitmap);
              // Get the Image's file name
              String fileNameSegments[] = imgPath.split("/");
              fileName = fileNameSegments[fileNameSegments.length - 1];
              // Put file name in Async Http Post Param which will used in Php web app
              params.put("filename", fileName);
          } else {
              Toast.makeText(this, R.string.noimageselected,
                      Toast.LENGTH_LONG).show();
          }
      } catch (Exception e) {
          Toast.makeText(this, R.string.erroroccurred, Toast.LENGTH_LONG)
                  .show();
      }
  }
  
  @Override
          protected String doInBackground(Void... params) {
              BitmapFactory.Options options = null;
              options = new BitmapFactory.Options();
              options.inSampleSize = 3;
              bitmap = BitmapFactory.decodeFile(imgPath, options);
              ByteArrayOutputStream stream = new ByteArrayOutputStream();
              // Must compress the Image to reduce image size to make upload easy
              bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
              byte[] byte_arr = stream.toByteArray();
              // Encode Image to String
              encodedString = Base64.encodeToString(byte_arr, 0);
              return "";
          }
  </code></pre>