1。使用BitmapFactory.Options裁图,指定最大宽和最大高。

public static Bitmap getBitmapFromMedia(Context context, String pathName, int maxWidth, int maxHeight) {
		Bitmap bitmap = null;
		BitmapFactory.Options options = new BitmapFactory.Options();
		try {
			options.inJustDecodeBounds = true;
			BitmapFactory.decodeFile(pathName, options);
			options.inJustDecodeBounds = false;

			int outputWidth = options.outWidth;
			int outputHeight = options.outHeight;
			if (outputWidth < maxWidth && outputHeight < maxHeight) {
				bitmap = BitmapFactory.decodeFile(pathName);
			} else {
				int inSampleSize = 0;
				int widthSmapleSize = (int) (outputWidth / maxWidth);
				int heightSmapleSize = (int) (outputHeight / maxHeight);
				MCLogUtil.i("ImageUtil", "outputWidth = " + outputWidth + " widthSmapleSize = " + widthSmapleSize);
				MCLogUtil.i("ImageUtil", "outputHeight = " + outputHeight + " heightSmapleSize = " + heightSmapleSize);
				if (widthSmapleSize >= heightSmapleSize) {
					inSampleSize = widthSmapleSize;
				} else {
					inSampleSize = heightSmapleSize;
				}
				if (inSampleSize < 3)
					options.inSampleSize = 2;
				else if (inSampleSize < 5)
					options.inSampleSize = 4;
				else if (inSampleSize < 8)
					options.inSampleSize = 8;
				else
					options.inSampleSize = inSampleSize;

				bitmap = BitmapFactory.decodeFile(pathName, options);
			}

		} catch (OutOfMemoryError oom) {
			System.gc();
			return null;
		} catch (Exception e) {
			return null;
		}
		return bitmap;
	}

inSampleSize的官方解释:For example, inSampleSize == 4 returns an p_w_picpath that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. 解释起来应该是:当inSampleSize为4时,一个2000*1000的图片,将被缩小为500*250,相应地,它的像素数和内存占用都被缩小为了原来的1/16,小于1的当1处理。

2。使用Matrix裁图。按短边裁图。

public static Bitmap resizeAccordShortestEdgeBitmap(Bitmap bitmap, int upperLimitSize) throws Exception {
		if (bitmap == null) {
			return null;
		}
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();

		float scaleWidth = 0f;
		float scaleHeight = 0f;
		if (width <= height) {
			float proportion = ((float) height) / ((float) width);
			int newHeight = (int) ((upperLimitSize) * proportion);
			scaleWidth = ((float) upperLimitSize) / width;
			scaleHeight = ((float) newHeight) / height;
		} else {
			float proportion = ((float) width) / ((float) height);
			int newWidth = (int) ((upperLimitSize) * proportion);
			scaleWidth = ((float) newWidth) / width;
			scaleHeight = ((float) upperLimitSize) / height;
		}

		Matrix matrix = new Matrix();
		// resize the bit map
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
		return resizedBitmap;
	}

3。质量压缩。使用官方压缩方式。

public boolean compress (Bitmap.CompressFormat format, int quality, OutputStream stream)
formatThe format of the compressed p_w_picpath
qualityHint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting
streamThe outputstream to write the compressed data.

例子:

boolean isCompressed = sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

PNG是无损的,将忽略质量设置。