【发布时间】:2015-07-01 08:41:42
【问题描述】:
在我的 android 应用程序中,我想将图像上传到服务器。服务器不接受大于2M的图片的问题。但用户可以选择大于2M的图片。
所以我想构建一个使图像小于 2M 的代码。
我有两种方法:
- 调整图像尺寸。如下:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
-
我也可以压缩图像
image.compress(Bitmap.CompressFormat.PNG, 10, fos);
这两种方法有什么区别?
【问题讨论】:
-
你为什么对我的问题投反对票?
标签: android image compression