Java tutorial
//package com.java2s; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; import android.text.TextUtils; public class Main { public static Bitmap fix(String path, int w, int h) { if (TextUtils.isEmpty(path)) return null; BitmapFactory.Options options = new Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); int imageH = options.outHeight; int imageW = options.outWidth; int scaleX = imageH / w; int scaleY = imageW / h; int scale = 1; if (scaleX >= scaleY & scaleY >= 1) { scale = scaleX; } if (scaleY >= scaleX & scaleX >= 1) { scale = scaleY; } options.inJustDecodeBounds = false; options.inSampleSize = scale; return BitmapFactory.decodeFile(path, options); } public static Bitmap fix(Context context, int rid, int w, int h) { BitmapFactory.Options options = new Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(context.getResources(), rid, options); int imageH = options.outHeight; int imageW = options.outWidth; int scaleX = imageH / w; int scaleY = imageW / h; int scale = 1; if (scaleX >= scaleY & scaleY >= 1) { scale = scaleX; } if (scaleY >= scaleX & scaleX >= 1) { scale = scaleY; } options.inJustDecodeBounds = false; options.inSampleSize = scale; return BitmapFactory.decodeResource(context.getResources(), rid, options); } }