Java tutorial
//package com.java2s; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.text.TextUtils; import java.lang.ref.WeakReference; public class Main { private static WeakReference<Bitmap> cprsBmpBySize(String path, int rqsW, int rqsH) { if (TextUtils.isEmpty(path)) return null; final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); options.inSampleSize = caculateInSampleSize(options, rqsW, rqsH); options.inJustDecodeBounds = false; return new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, options)); } /** * caculate the bitmap sampleSize * * @return inSampleSize */ private static int caculateInSampleSize(BitmapFactory.Options options, int rqsW, int rqsH) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (rqsW == 0 || rqsH == 0) return 1; if (height > rqsH || width > rqsW) { final int heightRatio = Math.round((float) height / (float) rqsH); final int widthRatio = Math.round((float) width / (float) rqsW); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } }