在我的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);
这两种方法之间有什么区别?
答案 0 :(得分:5)
图像尺寸调整意味着您将缩短图像的分辨率。假设用户选择1000 * 1000像素图像。你要将图像转换成300 * 300的图像。因此图像尺寸将减小。
图像压缩会降低图像的文件大小而不会影响分辨率。当然,降低文件大小会影响图像质量。有许多压缩算法可以减少文件大小而不会对图像质量产生太大影响。