Generating square cropped thumbnails in Android / Java.

June 30th 2013

It's not like me to overcomplicate things, and before a couple days ago I used to use a rather lengthy method to generate a thumbnail from a bitmap in Android.

The objective for me was to create a square cropped image from a Bitmap where I could specify the dimensions and everything looked good at the end of it.

Turns out, there's a much easier way than writing something complex.

Text Snippet:
Bitmap resized = ThumbnailUtils.extractThumbnail(sourceBitmap, width, height);

sourceBitmap should be a Bitmap object, whereas width / height should both be int.

To generate the thumbnail from a File reference, use something like this:

Text Snippet:
Bitmap resized = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), width, height);

As ever, Bitmap functions should be run on an AsyncTask.