//压缩图片
public Bitmap compressBitmap(int i) throws IOException {
// 尺寸压缩倍数,值越大,图片尺寸越小
int ratio = 2;
// 压缩Bitmap到对应尺寸
Bitmap bmp =BitmapFactory.decodeByteArray(items.get(i).getImageAsBytes(), 0, items.get(i).getImageAsBytes().length);
Bitmap result = Bitmap.createBitmap(bmp.getWidth() / ratio, bmp.getHeight() / ratio, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Rect rect = new Rect(0, 0, bmp.getWidth() / ratio, bmp.getHeight() / ratio);
canvas.drawBitmap(bmp, null, rect, null);
return result;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String path = Environment.getExternalStorageDirectory()+ "/Image/";
File dirFile = new File(path); //目录转化成文件夹
if (!dirFile .exists()) { //如果不存在,建立这个文件夹
dirFile .mkdirs();
}
//保存图片到文件夹
File file = new File(path, items.get(i).getMessage()+ ".jpg");
try {
FileOutputStream out = new FileOutputStream(file);
// 把压缩后的数据存放到out中
result.compress(Bitmap.CompressFormat.JPEG, 100 ,out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
网友评论