title: How to subsample/resize an image like in whatsapp
body: <p>When sending an image through whatsapp, you can see the image you are sending  in an imageview <strong>scaled very very well</strong></p>
  
  <p>for example, i sent two images to my friend </p>
  
  <ul>
  <li><p>size of first image  : <strong>1296 pixels</strong> X <strong>2304 pixels</strong></p></li>
  <li><p>size of second image : <strong>1920 pixels</strong> X <strong>1080 pixels</strong></p></li>
  </ul>
  
  <p><strong>this images are too big therefore whatsapp has to scale them before showing them to me in an imageview</strong></p>
  
  <ul>
  <li><p>size of first image after being scaled by whatapp <strong>333 pixels</strong> X <strong>339 pixels</strong></p></li>
  <li><p>size of second image after being scaled by whatsapp <strong>333 pixels</strong> X <strong>187 pixels</strong></p></li>
  </ul>
  
  <p>As you can see the width is the same only height differs. i have tried to figure out how whatapp scales this images, but my methods <strong>gives me an image with a different dimensions far from whatsapp's ones</strong></p>
  
  <p><strong>Method 1</strong></p>
  
  <pre><code>private void resizeImage(){
  
  Uri selectedImage = imageReturnedIntent.getData();
                        d("image url is " + selectedImage);
                        InputStream imageStream = getContentResolver().openInputStream(selectedImage);
                         BitmapFactory.decodeStream(imageStream,null, options);
                        imageHeight = options.outHeight;
                        imageWidth = options.outWidth;
  
                        options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
                        options.inJustDecodeBounds = false;
                        bitmap = BitmapFactory.decodeStream(imageStream,null, options);
  
  }
  private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth , int reqHeght){
  
      int height = options.outHeight;
      int width = options . outWidth;
      d("width is "+ width + " height is "+ height);
      d("required width is "+ reqWidth + " required height "+ reqHeght);
      int inSampleSize = 1;
  
      if(height &gt; reqHeght || width &gt; reqWidth){
  
          int heightRatio = Math.round((float)height/(float)reqHeght);
          d("height ratio is "+ heightRatio);
  
          int widthRatio = Math.round((float)width / (float) reqWidth);
          d("width ratio is "+widthRatio);
  
          inSampleSize = (heightRatio &gt; widthRatio)? heightRatio :widthRatio;
      }
      d(" insample size is "+ inSampleSize);
      return inSampleSize;
  }
  </code></pre>
  
  <p><strong>Output for first image using the above method :</strong> Smaller(&lt; 333) width, very big height(>339. it's 579!!)</p>
  
  <p><strong>Second method</strong></p>
  
  <pre><code>private Bitmap scaleToFitWidth(Bitmap b, int width){
    float factor = width/b.getWidth(); // width is 333
  
    return Bitmap.createScaledBitmap(b, width,(int)(b.getHeight() * factor),true)
  }
  </code></pre>
  
  <p><strong>Output of first image in second method:</strong> image height is very very big!</p>
  
  <p><strong>Question</strong> Does anyone knows how to scale images very well like whatapp <strong>Across all devices</strong>?(i would like to be the same as whatsapp if possible)</p>
  
  <p><strong>EDIT: whatsapp Images</strong></p>
  
  <p><strong>My huawel phone</strong></p>
  
  <p><a href="https://i.stack.imgur.com/oyvrr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oyvrr.png" alt=" image is huawel"></a></p>
  
  <p><strong>samsung tablet</strong></p>
  
  <p><a href="https://i.stack.imgur.com/8r3dT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8r3dT.png" alt="image in samsung talet"></a></p>