//图片压缩方法
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;
}
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;
}