code: public static Bitmap decodeSampledBitmapFromFile(Resources res, int resId,
                                                       int reqWidth, int reqHeight) {
  
          // First decode with inJustDecodeBounds=true to check dimensions
          final BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          BitmapFactory.decodeResource(res, resId, options);
  
          // Calculate inSampleSize
          options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  
          // Decode bitmap with inSampleSize set
          options.inJustDecodeBounds = false;
          return BitmapFactory.decodeResource(res, resId, options);
      }
docs: Utility method to create a downsampled bitmap from a given image file preserving aspect ratio.
  
  @param res       Resource to get asset from.
  @param resId     the id of the image asset.
  @param reqWidth  the required width for the bitmap
  @param reqHeight the required height for the bitmap.
  @return Bitmap from file that has been downsampled to fit width and height.