title: Android: Compress Bitmap from Uri
body: <p>Take a picture with the normal smartphone camera.</p>
  
  <p>Ok I've been Googling this for a while now, and everyone seems to use something like the following:             </p>
  
  <pre><code>Bitmap bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(fileUri));
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  bm.compress(Bitmap.CompressFormat.JPEG, 25, out);
  Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
  </code></pre>
  
  <p>I use this to check the file size:</p>
  
  <pre><code>@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
  protected int sizeOf(Bitmap data) {
      if (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.HONEYCOMB_MR1) {
          return data.getRowBytes() * data.getHeight();
      } else {
          return data.getByteCount();
      }
  }
  </code></pre>
  
  <p>The Bitmap is not getting any smaller, before and after:</p>
  
  <pre><code>Log.d("image", sizeOf(bm)+"");
  Log.d("image", sizeOf(decoded)+"");
  </code></pre>
  
  <p>Results:</p>
  
  <pre><code>11-05 02:51:52.739: D/image(2558): 20155392
  11-05 02:51:52.739: D/image(2558): 20155392
  </code></pre>
  
  <p>Pointers?</p>
  
  <p><strong>Answer:</strong> </p>
  
  <pre><code>BitmapFactory.Options options = new BitmapFactory.Options();
  options.inSampleSize = 50;
  Bitmap bmpSample = BitmapFactory.decodeFile(fileUri.getPath(), options);
  
  Log.d("image", sizeOf(bmpPic)+"");
  
  ByteArrayOutputStream out = new ByteArrayOutputStream();                
  bmSample.compress(Bitmap.CompressFormat.JPEG, 1, out);
  byte[] byteArray = out.toByteArray();
  
  Log.d("image", byteArray.length/1024+"");
  </code></pre>