Java tutorial
//package com.java2s; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; public class Main { public static int[] decodeSampleSize(String path, float reqWidth, float reqHeight) { Options opts = new Options(); opts.inJustDecodeBounds = true; opts.inPreferredConfig = Config.RGB_565; BitmapFactory.decodeFile(path, opts); opts.inSampleSize = calculateInSampleSize(opts, reqWidth, reqHeight); BitmapFactory.decodeFile(path, opts); return new int[] { opts.outWidth, opts.outHeight }; } public static int calculateInSampleSize(Options options, float reqWidth, float reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; while ((halfHeight / inSampleSize) > reqHeight || (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } }