title: BitmapFactory.decodeStream out of memory despite using reduced sample size
body: <p>I have read many related posts concerning memory allocation problems with decoding bitmaps, but am still unable to find the solution to the following problem even after using the code provided in the official website.</p>
  
  <p>Here is my code: </p>
  
  <pre><code>public static Bitmap decodeSampledBitmapFromResource(InputStream inputStream, int reqWidth, int reqHeight) {
  
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      byte[] buffer = new byte[1024];
      int len;
      try {
          while ((len = inputStream.read(buffer)) &gt; -1) {
          baos.write(buffer, 0, len);
          }
          baos.flush();
          InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
          InputStream is2 = new ByteArrayInputStream(baos.toByteArray());
  
          final BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          BitmapFactory.decodeStream(is1, null, options);
  
          options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
          options.inPurgeable = true;
          options.inInputShareable = true;
          options.inJustDecodeBounds = false;
          options.inPreferredConfig = Bitmap.Config.ARGB_8888;
          return BitmapFactory.decodeStream(is2, null, options);
  
      } catch (Exception e) {
          e.printStackTrace();
  
          return null;
      }
  }
  
  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) {
  
          // Calculate ratios of height and width to requested height and width
          final int heightRatio = Math.round((float) height / (float) reqHeight);
          final int widthRatio = Math.round((float) width / (float) reqWidth);
  
          // Choose the smallest ratio as inSampleSize value, this will guarantee
          // a final image with both dimensions larger than or equal to the
          // requested height and width.
          inSampleSize = heightRatio &lt; widthRatio ? heightRatio : widthRatio;
      }
  
  
      return inSampleSize;
  }
  
  bitmap = decodeSampledBitmapFromResource(inputStream, 600, 600); 
  </code></pre>
  
  <p>I am getting "Out of memory error on a 3250016 - byte allocation" in this line:</p>
  
  <pre><code>return BitmapFactory.decodeStream(is2, null, options);
  </code></pre>
  
  <p>It would seem to me that 3.2 MB is small enough to be allocated. Where am I going wrong? How can I solve this?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After looking into this solution <a href="https://stackoverflow.com/questions/10314527/caused-by-java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget/10408877#10408877">HERE</a> by N-Joy it works fine with Required size 300 but my required size is 800, so i am still getting the error.</p>