Android缩小大图像尺寸
问题描述:
如果手机上有图像,我想发送到1mb或更高的服务器,我希望将该图像缩小并缩小至较小的尺寸,而不会干扰原始图像。我知道如何从电话中获得答案,并且我也知道如何将它发送给服务器,我只需要知道如何缩小图像,因为大于1mb的大文件会给我带来问题。Android缩小大图像尺寸
答
可以缩放位图如下所示:
// Load in your bitmap here, I'll leave this detail up to you
Bitmap _bitmapPreScale = BitmapFactory.decodeResource(resources, fieldValue);
int oldWidth = _bitmapPreScale.getWidth();
int oldHeight = _bitmapPreScale.getHeight();
int newWidth = width; // whatever your desired width and height are
int newHeight = height;
// calculate the scale
float scaleWidth = ((float) newWidth)/oldWidth;
float scaleHeight = ((float) newHeight)/oldHeight;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap _bitmapScaled = Bitmap.createBitmap(_bitmapPreScale, 0, 0, oldWidth, oldHeight, matrix, true);
有没有办法检查所选图像的文件大小。就像文件大小大于1mb或1024kb做上面发布的内容一样,对于较小的文件大小的图像就不会出现这些高清图像的问题。谢谢 – user516883 2010-11-23 21:42:00