If there is a image on my phone that I 开发者_高级运维want to send up to a server thats 1mb or higher I want to take that image and shrink it to a smaller size without messing with original image in anyway. I know how to get a from the phone and I also know how to send it up to a server, I just need to know how to make the image smaller because large files higher than 1mb are giving me issues.
You can scale Bitmaps like so:
// 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);
From your original Bitmap, you can use createScaledBitmap(...)
However, the difficulty might be to open the large image in the first place.
精彩评论