title: Downsample image below size limit before upload
body: <p>There's a 2MB limit on the server where I need to upload an image.</p>
  
  <p>I'm using this method to downsample a Bitmap <a href="https://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue">Strange out of memory issue while loading an image to a Bitmap object</a></p>
  
  <p>within this method </p>
  
  <pre><code>public InputStream getPhotoStream(int imageSizeBytes) throws IOException {
          int targetLength = 1500;
          ByteArrayOutputStream photoStream;
          byte[] photo;
          Bitmap pic;
          final int MAX_QUALITY = 100;
          int actualSize = -1;
          do {
              photo = null;
              pic = null;
              photoStream = null;
  
              //this calls the downsampling method
              pic = getPhoto(targetLength);
  
              photoStream = new ByteArrayOutputStream();
              pic.compress(CompressFormat.JPEG, MAX_QUALITY, photoStream);
              photo = photoStream.toByteArray();
              actualSize = photo.length;
              targetLength /= 2;
          } while (actualSize &gt; imageSizeBytes);
          return new ByteArrayInputStream(photo);
  }
  </code></pre>
  
  <p>This throws OutOfMemoryError on the second iteration. How could I downsample the image below a certain size limit?</p>