Here you can find the source of decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight)
public static Bitmap decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight)
//package com.java2s; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.text.TextUtils; public class Main { public static Bitmap decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight) { if (TextUtils.isEmpty(filePath)) { return null; }/*from ww w . j av a 2 s. c o m*/ final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; options.inPurgeable = true; options.inInputShareable = true; BitmapFactory.decodeFile(filePath, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; try { return BitmapFactory.decodeFile(filePath, options); } catch (Throwable e) { e.printStackTrace(); } return null; } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if ((reqWidth > 0 && reqHeight > 0) && (height > reqHeight || width > reqWidth)) { if (width > height) { inSampleSize = Math.round((float) height / (float) reqHeight); } else { inSampleSize = Math.round((float) width / (float) reqWidth); } } return 0 == inSampleSize ? 1 : inSampleSize; } }