title: Scaling down Images in android
body: <p>I am implementing an application in which an activity shows a image view with full screen. Now i have some images downloaded in application's file directory. I want to set image to image view in that activity for which i am scaling down the larger images. I am getting OutOfMemory error after 10 to 15 mins of using application. error is in decodeFile of BitmapFactory. Here is my code :</p>
  
  <pre><code>public static int calculateInSampleSize(
          BitmapFactory.Options options, int reqWidth, int reqHeight) {
      // Raw height and width of image
      final int height = options.outHeight;
      final int width = options.outWidth;
      int inSampleSize = 1;
  
      if (height &gt; reqHeight || width &gt; reqWidth) {
          if (width &gt; height) {
              inSampleSize = Math.round((float)height / (float)reqHeight);
          } else {
              inSampleSize = Math.round((float)width / (float)reqWidth);
          }
      }
      return inSampleSize;
  }
  
  public static Bitmap decodeSampledBitmapFromFile(String imagePath,
          int reqWidth, int reqHeight) {
  
      // First decode with inJustDecodeBounds=true to check dimensions
      final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(imagePath, options);
  
      // Calculate inSampleSize
      options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  
      // Decode bitmap with inSampleSize set
      options.inJustDecodeBounds = false;
      return BitmapFactory.decodeFile(imagePath, options);
  
  }
  </code></pre>
  
  <p><strong>Right now i am giving reqHeight = 500 and reqWidth = 500</strong></p>
  
  <p>This code is given in Android guidelines page. Is there anything i am missing, if so please help me out. I tried lots of things but not getting any solution. </p>