Android收缩大图像尺寸

时间:2010-11-23 21:25:24

标签: android image

如果我的手机上有想要发送到1mb或更高的服务器的图像,我想拍摄该图像并将其缩小到较小的尺寸而不会弄乱原始图像。我知道如何通过手机获取,我也知道如何将其发送到服务器,我只需要知道如何使图像更小,因为大于1mb的大文件给我带来了问题。

2 个答案:

答案 0 :(得分:1)

您可以像这样缩放位图:

// 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);

答案 1 :(得分:0)

从原始位图中,您可以使用createScaledBitmap(...) 但是,困难可能是open the large image in the first place