title: Android BitmapFactory.Options.inSampleSize casuse application crash
body: <p>We are working on a Android app that captures images and converts the same to Bitmap to be posted to a Rest service.</p>
  
  <p>While creating the Bitmap we have BitmapFactory.Options.isSample Size configured to value of 4 and this works fine on all device except for HTC One. The image in HTC one is distorted.</p>
  
  <p>When we change the value to 1  it shows a good image on HTC one but the application crashes on all other devices because of Memory issues.</p>
  
  <p>The issue is related to Heap size available. </p>
  
  <ol>
  <li>What is the reason that HTC one shows a distorted image on  HTC one.</li>
  <li>Why is application crashing on S3 which also has the same build
  version of 16 but crashes due to heap size unavailability.</li>
  <li>Why does all lower version phone like droid works fine for sample
  size of 4.</li>
  <li>Whats the best way to check available memory space for an app and
  use the available for working with BITMAP.</li>
  </ol>
  
  <p>When we configure android:largeHeap in manifest file this seems to work but it does not seem to be a dependable solution.</p>
  
  <hr>
  
  <p>I was able to find the size of the picture taken using camera parameters and then arrive at optimal sample size of bitmap. The below code solves issue. But is it right way of doing or should i need to compute sample size  too instead of going with 1 and 4.</p>
  
  <pre><code>private Bitmap createImaegBitmap(byte[] data)
      {
          BitmapFactory.Options opt;
  
          opt = new BitmapFactory.Options();
          opt.inTempStorage = new byte[16 * 1024];
          Parameters parameters = ((ReceiptCollection)getApplication()).getParams();
          Size size = parameters.getPictureSize();
  
          int height11 = size.height;
          int width11 = size.width;
          float mb = (width11 * height11) / 1024000;
  
          if (mb &gt; 4f)
              opt.inSampleSize = 4;
          else if (mb &gt; 3f)
              opt.inSampleSize = 1;
          Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,opt);
          return bitmap;
      }
  </code></pre>