Bitmap bitmap =null;
//通过工厂类,解释路径,获得图片
bitmap = BitmapFactory.decodeFile(filePath);
//get width & height
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap destBitmap = null;
//destination height & width
float destH = height;
float destW = width;
//Scale new/old
float temp1 = 0;
float temp2 = 0;
//compress height below 1024
if (height > 1024) {
destH = 1024;
temp1 = ((float) destH) / ((float) height);
destW = width * temp1;
if (destW > 1024) {
temp1 = ((float) 1024) / ((float) destW);
destW = 1024;
destH = destH * temp1;
}
}else{
if (width > 1024) {
destW = 1024;
temp2 = ((float) destW) / ((float) width);
destH = height * temp2;
}
}
try {
//Creates a new bitmap, scaled from an existing bitmap, when possible
destBitmap = Bitmap.createScaledBitmap(bitmap, (int) destW, (int)destH, true);
destBitmap = switchColor(destBitmap);
//save new bitmap
OutputStream outStream = null;
outStream = new FileOutputStream(new File(filePath));
//Write a compressed version of the bitmap to the specified outputstream.
destBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}