android 图片压缩后比压缩前还大很多,不知道哪里出问题了

移动开发 码拜 9年前 (2015-11-28) 1723次浏览
//图片压缩方法
public static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while ((baos.toByteArray().length/1024>100)&&(options>0)) {  //循环判断假如压缩图片能否大于100kb,大于继续压缩
Log.i(“LENGTH”, “”+baos.toByteArray().length/1024);
Log.i(“LENGTH”, “”+options);
options = (int)(options*0.9);
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
//        Bitmap bitmap = BitmapFactory.decodeStream(isBm);
try {
isBm.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
// 将压缩的bitmap保存到sdcard文件夹img_interim
public static File saveMyBitmap(String filename, Bitmap bit) {
String sdPath = Environment.getExternalStorageDirectory().getPath();
File dir = new File(sdPath+File.separator+”img_interim”);
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(sdPath + File.separator + “img_interim” + File.separator + filename);
try {
file.createNewFile();
FileOutputStream fOut = null;
fOut = new FileOutputStream(file);
bit.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (IOException e1) {
file = null;
}
return file;
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明android 图片压缩后比压缩前还大很多,不知道哪里出问题了
喜欢 (0)
[1034331897@qq.com]
分享 (0)