title: Quality problems when resizing an image at runtime
body: <p>I have a image file on the disk and I am resizing the file and saving it back to disk as a new image file.  For the sake of this question, I am not bringing them into memory in order to display them on the screen, only to resize them and resave them.  This all works just fine.  However, the scaled images have artifacts on them like shown here: <a href="https://stackoverflow.com/questions/2041207/android-quality-of-the-images-resized-in-runtime">android: quality of the images resized in runtime</a></p>
  
  <p>They are saved with this distortion, as I can pull them off the disk and look at them on my computer and they still have the same issue.</p>
  
  <p>I am using code similar to this <a href="https://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966">Strange out of memory issue while loading an image to a Bitmap object</a> to decode the bitmap into memory:</p>
  
  <pre><code>BitmapFactory.Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;
  BitmapFactory.decodeFile(imageFilePathString, options);
  
  int srcWidth = options.outWidth;
  int srcHeight = options.outHeight;
  int scale = 1;
  
  while(srcWidth / 2 &gt; desiredWidth){
     srcWidth /= 2;
     srcHeight /= 2;
     scale *= 2;
  }
  
  options.inJustDecodeBounds = false;
  options.inDither = false;
  options.inSampleSize = scale;
  Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(imageFilePathString, options);
  </code></pre>
  
  <p>Then I am doing the actual scaling with:</p>
  
  <pre><code>Bitmap scaledBitmap = Bitmap.createScaledBitmap(sampledSrcBitmap, desiredWidth, desiredHeight, false);
  </code></pre>
  
  <p>Lastly, the new resized image is saved to disk with:</p>
  
  <pre><code>FileOutputStream out = new FileOutputStream(newFilePathString);
  scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
  </code></pre>
  
  <p>Then, as I mentioned, if I pull that file off the disk and look at it, it has that quality issue linked above and looks terrible.  If I skip the createScaledBitmap and just save the sampledSrcBitmap right back to disk there is no problem, it seems to only happen if the size changes.</p>
  
  <p>I have tried, as you can see in the code, setting inDither to false as mentioned here <a href="http://groups.google.com/group/android-developers/browse_thread/thread/8b1abdbe881f9f71" rel="nofollow noreferrer">http://groups.google.com/group/android-developers/browse_thread/thread/8b1abdbe881f9f71</a> and as mentioned in the very first linked post above.  That didn't change anything.  Also, in the first post I linked, Romain Guy said: </p>
  
  <blockquote>
    <p>Instead of resizing at drawing time
    (which is going to be very costly),
    try to resize in an offscreen bitmap
    and make sure that Bitmap is 32 bits
    (ARGB888).</p>
  </blockquote>
  
  <p>However, I have no idea how to make sure the Bitmap stays as 32 bits through the whole process.</p>
  
  <p>I have also read a couple other articles such as this <a href="http://android.nakatome.net/2010/04/bitmap-basics.html" rel="nofollow noreferrer">http://android.nakatome.net/2010/04/bitmap-basics.html</a> but they all seemed to be addressing drawing and displaying the Bitmap, I just want to resize it and save it back to disk without this quality problem.</p>
  
  <p>Thanks much</p>