beforeBitmap.compress(Bitmap.CompressFormat.JPEG, options, bos);
已知 以上代码是对于图片进行压缩 那如果压缩的是gif图呢 需要换成流去压缩吗
用该代码去压缩gif 生成的是gif结尾的图片 可是栋不起来了 这点比较疑惑 希望各位大佬点醒一下
我的压缩代码如下
String directoryPrefix = context.getExternalFilesDir("").getAbsolutePath()+SLASH;
String imagepath = directoryPrefix +"sugar"+ SLASH + "image"+SLASH;
File file = new File(imagepath);
if (!file.exists()) {
file.mkdirs();
}
// 可以捕获内存缓冲区的数据,转换成字节数组。
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (beforeBitmap != null) {
// 第一个参数:图片压缩的格式;第二个参数:压缩的比率;第三个参数:压缩的数据存放到bos中
beforeBitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
// 循环判断压缩后的图片大小是否满足要求,这里限制100kb,若不满足则继续压缩,每次递减10%压缩
int options = quality; // 70
while (bos.toByteArray().length / 1024 > maxSize) {
bos.reset();// 置为空
beforeBitmap.compress(Bitmap.CompressFormat.JPEG, options, bos);
options -= 5; // 每次减少5的压缩质量
}
// 转化为文件
try {
File compressImageFile = null;
if (isGif ){
compressImageFile = File.createTempFile("image", ".gif", file);
}else {
compressImageFile = File.createTempFile("image", ".jpg", file);
}
// 写入 绝对路径
uncompressImageWithLowQuality(compressImageFile.getAbsolutePath() , bos);
return compressImageFile.getAbsolutePath();
} catch (IOException e) {
SLog.e("createTempFile error is e ="+ e.getMessage());
e.printStackTrace();
}
// 从bos中将数据读出来 转换成图片(位图)
// ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
// Bitmap afterBitmap = BitmapFactory.decodeStream(bis);
// return afterBitmap;
}
return null;
您的每一个用心回答,都会让这个世界变得更美好一些!