Here you can find the source of compress(String filePath, int width, int height)
public static Bitmap compress(String filePath, int width, int height)
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; public class Main { public static Bitmap compress(String filePath, int width, int height) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(filePath, options); int bmpWidth = options.outWidth; int bmpHeight = options.outHeight; options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(filePath, options); float scaleWidth = bmpWidth; float scaleHeight = bmpHeight; // w ww.j a va 2 s.c o m if (bmpWidth > width) { scaleWidth = width / scaleWidth; } else { scaleWidth = 1; } if (bmpHeight > height) { scaleHeight = height / scaleHeight; } else { scaleHeight = 1; } Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, false); if (bitmap != resizeBitmap) { bitmap.recycle(); } return resizeBitmap; } }