Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.BitmapFactory; public class Main { public static int getSampleSize(BitmapFactory.Options options, int maxWidth, int maxHeight) { // raw height and width of image int rawWidth = options.outWidth; int rawHeight = options.outHeight; // calculate best sample size int inSampleSize = 0; if (rawHeight > maxHeight || rawWidth > maxWidth) { float ratioWidth = (float) rawWidth / maxWidth; float ratioHeight = (float) rawHeight / maxHeight; inSampleSize = (int) Math.min(ratioHeight, ratioWidth); } inSampleSize = Math.max(1, inSampleSize); return inSampleSize; } }