title: How whatsapp/Instagram or other compresses the image before uploading it to server?
body: <p>I used below method to compress image before uploading it to server. </p>
  
  <p>To summarize the below code, Initially i take image from phone/sdcard, convert it to bitmap and carryout scaling and compression on bitmap. then i save the compressed bitmap on the disk as image in new location.</p>
  
  <p>This method does compress the image from 3mb - 5mb to just in 30-100kb. (when i check the compressed image size on sdcard)</p>
  
  <p>Then i send the newly compressed image of 30-100kb to upload on to the server by converting it to bitmap string ( by Base64.encodeToString() )</p>
  
  <p>I thought it will work fine. But when i check the uploaded image on server, the size of image is same as original image size (original size before compressed) instead of compressed 30-100kb size image.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public String compressImage(String imageUri)
    {
      String filePath = getRealPathFromURI(imageUri);
      Bitmap scaledBitmap = null;
  
      BitmapFactory.Options options = new BitmapFactory.Options();
  
      options.inJustDecodeBounds = true;
      Bitmap bmp = BitmapFactory.decodeFile(filePath, options);
  
      int actualHeight = options.outHeight;
      int actualWidth = options.outWidth;
  
      float maxHeight = 816.0f;
      float maxWidth = 612.0f;
      float imgRatio = actualWidth / actualHeight;
      float maxRatio = maxWidth / maxHeight;
  
      if (actualHeight &gt; maxHeight || actualWidth &gt; maxWidth) {
          if (imgRatio &lt; maxRatio) {               imgRatio = maxHeight / actualHeight;                actualWidth = (int) (imgRatio * actualWidth);               actualHeight = (int) maxHeight;             } else if (imgRatio &gt; maxRatio) {
              imgRatio = maxWidth / actualWidth;
              actualHeight = (int) (imgRatio * actualHeight);
              actualWidth = (int) maxWidth;
          } else {
              actualHeight = (int) maxHeight;
              actualWidth = (int) maxWidth;
  
          }
      }
  
      options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
  
      options.inJustDecodeBounds = false;
  
      options.inPurgeable = true;
      options.inInputShareable = true;
      options.inTempStorage = new byte[16 * 1024];
  
      try {
          bmp = BitmapFactory.decodeFile(filePath, options);
      } catch (OutOfMemoryError exception) {
          exception.printStackTrace();
  
      }
      try {
          scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight,Bitmap.Config.ARGB_8888);
      } catch (OutOfMemoryError exception) {
          exception.printStackTrace();
      }
  
      float ratioX = actualWidth / (float) options.outWidth;
      float ratioY = actualHeight / (float) options.outHeight;
      float middleX = actualWidth / 2.0f;
      float middleY = actualHeight / 2.0f;
  
      Matrix scaleMatrix = new Matrix();
      scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
  
      Canvas canvas = new Canvas(scaledBitmap);
      canvas.setMatrix(scaleMatrix);
      canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
  
      ExifInterface exif;
      try {
          exif = new ExifInterface(filePath);
  
          int orientation = exif.getAttributeInt(
                  ExifInterface.TAG_ORIENTATION, 0);
          Log.d("EXIF", "Exif: " + orientation);
          Matrix matrix = new Matrix();
          if (orientation == 6) {
              matrix.postRotate(90);
              Log.d("EXIF", "Exif: " + orientation);
          } else if (orientation == 3) {
              matrix.postRotate(180);
              Log.d("EXIF", "Exif: " + orientation);
          } else if (orientation == 8) {
              matrix.postRotate(270);
              Log.d("EXIF", "Exif: " + orientation);
          }
          scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
                  scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,
                  true);
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      FileOutputStream out = null;
      String filename = getFilename();
      try {
          out = new FileOutputStream(filename);
          scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
  
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
  return filename;
  }
  
  public String getFilename() {
  File file = new File(Environment.getExternalStorageDirectory().getPath(), "MyFolder/Images");
  if (!file.exists()) {
      file.mkdirs();
  }
  String uriSting = (file.getAbsolutePath() + "/" +  System.currentTimeMillis() + ".jpg");
      return uriSting;}
  
  private String getRealPathFromURI(String contentURI) {
      Uri contentUri = Uri.parse(contentURI);
      Cursor cursor = getContentResolver().query(contentUri, null, null, null, null);
      if (cursor == null) {
          return contentUri.getPath();
      } else {
          cursor.moveToFirst();
          int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
          return cursor.getString(index);
      }
  }
  
  public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
  final int height = options.outHeight;
  final int width = options.outWidth;
  int inSampleSize = 1;
  
  if (height &gt; reqHeight || width &gt; reqWidth) {
      final int heightRatio = Math.round((float) height/ (float) reqHeight);
      final int widthRatio = Math.round((float) width / (float) reqWidth);
      inSampleSize = heightRatio &lt; widthRatio ? heightRatio : widthRatio;      }       final float totalPixels = width * height;       final float totalReqPixelsCap = reqWidth * reqHeight * 2;       while (totalPixels / (inSampleSize * inSampleSize) &gt; totalReqPixelsCap) {
      inSampleSize++;
  }
      return inSampleSize;
  </code></pre>
  
  <p>}</p>
  
  <p>Am i going wrong somewhere in this above code ? Is there any better method to compress the image. I am wondering how can uncompressed image(bitmap) is getting uploaded instead of compressed one.</p>
  
  <p>How does the whatsapp/instagram/facebook compresses the image before uploading on to the server. Is there any library for it. Thank you.</p>