Java tutorial
//package com.java2s; import java.io.InputStream; import android.graphics.BitmapFactory; public class Main { /** * Compute the inSampleSize for BitmapFactory.Option to scale down bitmap size into required size. * @param options * @param requireWidth * @param requireHeight * @return */ public static int computeInSampleSize(BitmapFactory.Options options, int requireWidth, int requireHeight) { int inSampleSize = 1; final int height = options.outHeight; final int width = options.outWidth; if (height > requireHeight || width > requireWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; while (((halfHeight / inSampleSize) > requireHeight) && ((halfWidth / inSampleSize) > requireWidth)) { inSampleSize *= 2; } } return inSampleSize; } public static int computeInSampleSize(InputStream inputStream, int requireWidth, int requireHeight) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(inputStream, null, options); return computeInSampleSize(options, requireWidth, requireHeight); } }