我使用此功能在上傳之前,以減少圖像的大小,但通過以下方法我的文件大小增加如何減少圖像文件大小
下面的代碼我的文件大小---使用前> 157684
使用此代碼我的文件大小後-----> 177435
有一個人幫助我,我請如何上傳前減少文件大小到服務器
代碼:
public File saveBitmapToFile(File file){
try {
// BitmapFactory options to downsize the image
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inSampleSize = 6;
// factor of downsizing the image
FileInputStream inputStream = new FileInputStream(file);
//Bitmap selectedBitmap = null;
BitmapFactory.decodeStream(inputStream, null, o);
inputStream.close();
// The new size we want to scale to
final int REQUIRED_SIZE=75;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while(o.outWidth/scale/2 >= REQUIRED_SIZE &&
o.outHeight/scale/2 >= REQUIRED_SIZE) {
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
inputStream = new FileInputStream(file);
Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, o2);
inputStream.close();
// here i override the original image file
file.createNewFile();
FileOutputStream outputStream = new FileOutputStream(file);
selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100 , outputStream);
return file;
} catch (Exception e) {
return null;
}
}
Bitmap.CompressFormat.JPEG,100,outputStream爲Bitmap.CompressFormat.JPEG,REQUIRED_SIZE,outputStream –
100質量好0爲牀品質。所以選擇按質量比壓縮,以減少您的文件大小 –
http://stackoverflow.com/questions/8417034/how-to-make-bitmap-compress-without-change-the-bitmap-size –