Quality problems when resizing an image at runtime Quality problems when resizing an image at runtime android android

Quality problems when resizing an image at runtime


After experimenting I have finally found a way to do this with good quality results. I'll write this up for anyone that might find this answer helpful in the future.

To solve the first problem, the artifacts and weird dithering introduced into the images, you need to insure your image stays as a 32-bit ARGB_8888 image. Using the code in my question, you can simply add this line to the options before the second decode.

options.inPreferredConfig = Bitmap.Config.ARGB_8888;

After adding that, the artifacts were gone but edges throughout the images came through jagged instead of crisp. After some more experimentation I discovered that resizing the bitmap using a Matrix instead of Bitmap.createScaledBitmap produced much crisper results.

With those two solutions, the images are now resizing perfectly. Below is the code I am using in case it benefits someone else coming across this problem.

// Get the source image's dimensionsBitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(STRING_PATH_TO_FILE, options);int srcWidth = options.outWidth;int srcHeight = options.outHeight;// Only scale if the source is big enough. This code is just trying to fit a image into a certain width.if(desiredWidth > srcWidth)    desiredWidth = srcWidth;// Calculate the correct inSampleSize/scale value. This helps reduce memory use. It should be a power of 2// from: https://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966int inSampleSize = 1;while(srcWidth / 2 > desiredWidth){    srcWidth /= 2;    srcHeight /= 2;    inSampleSize *= 2;}float desiredScale = (float) desiredWidth / srcWidth;// Decode with inSampleSizeoptions.inJustDecodeBounds = false;options.inDither = false;options.inSampleSize = inSampleSize;options.inScaled = false;options.inPreferredConfig = Bitmap.Config.ARGB_8888;Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(STRING_PATH_TO_FILE, options);// ResizeMatrix matrix = new Matrix();matrix.postScale(desiredScale, desiredScale);Bitmap scaledBitmap = Bitmap.createBitmap(sampledSrcBitmap, 0, 0, sampledSrcBitmap.getWidth(), sampledSrcBitmap.getHeight(), matrix, true);sampledSrcBitmap = null;// SaveFileOutputStream out = new FileOutputStream(NEW_FILE_PATH);scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);scaledBitmap = null;

EDIT: After continual work on this I have found that the images still aren't 100% perfect. I'll make an update if I can improve it.

Update: After revisting this, I found this question on SO and there was an answer that mentioned the inScaled option. This helped with the quality as well so I added updated the answer above to include it. I also now null the bitmaps after they are done being used.

Also, as a side note, if you are using these images in a WebView, make sure you take this post into consideration.

Note: you should also add a check to make sure the width and height are valid numbers (not -1). If they are, it will cause the inSampleSize loop to become infinite.


In my situation I am drawing the image to the screen. Here's what I did to get my images to look correct (a combination of littleFluffyKitty's answer, plus a few other things).

For my options when I actually load the image (using decodeResource) I set the following values:

    options.inScaled = false;    options.inDither = false;    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

When I actually draw the image, I set up my paint object like this:

    Paint paint = new Paint();    paint.setAntiAlias(true);    paint.setFilterBitmap(true);    paint.setDither(true);

Hopefully someone else finds that useful too. I wish there were just options for "Yes, let my resized images look like garbage" and "No, please don't force my users to gouge their eyes out with spoons" instead of all the myriad of different options. I know they want to give us lots of control, but maybe some helper methods for common settings could be useful.


I created simple library based on littleFluffyKitty answer which does resize and does some other things like crop and rotation so please free to use it and improve it - Android-ImageResizer.