0

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.

user516883
  • 7,330
  • 22
  • 65
  • 112

2 Answers2

1

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);
Bryan Denny
  • 26,451
  • 32
  • 103
  • 124
  • Is there a way to check the file size of the selected image. Like if file size is > 1mb or 1024kb do what you posted above, I have no problems with smaller file size images just those HD images. Thanks – user516883 Nov 23 '10 at 21:42
  • Ok i think i can just use this File file = new File("infilename"); then call the file.length i think that will do the trick – user516883 Nov 23 '10 at 21:54
0

From your original Bitmap, you can use createScaledBitmap(...) However, the difficulty might be to open the large image in the first place.

Community
  • 1
  • 1
rds
  • 24,304
  • 16
  • 97
  • 124