鲁班压缩大家应该也是非常熟悉的,这里用递归的方法一张一张压缩,所以如果图片数量较多的话全部压缩完会比较慢,可以根据自己的情况做修改。
原项目:https://github.com/Curzibn/Luban
// compile 'top.zibin:Luban:1.0.9'
importandroid.content.Context;
importandroid.util.Log;
importcom.mxkj.yuanyintang.activity.PublishActivity;
importjava.io.File;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.List;
importtop.zibin.luban.Luban;
importtop.zibin.luban.OnCompressListener;
// compile 'top.zibin:Luban:1.0.9'
public classIMGCompressUtils {
public static intcount;
public staticListfiles=newArrayList<>();
public staticListCompressorImage1(intnum, finalContext context, finalList path, finalCompressCallback callback)throwsIOException {
if(num ==0) {
files.clear();
count= num;
}
File actualImage =newFile(path.get(count));
Luban.with(context)
.load(actualImage)//传人要压缩的图片
.putGear(3)
.setCompressListener(newOnCompressListener() {//设置回调
@Override
public voidonStart() {
}
@Override
public voidonSuccess(File file) {
Log.e("TAG","压缩第几张: "+count+"====="+ file.getAbsolutePath());
files.add(file);
Log.e("TAG","files.size: "+files.size() );
count++;
if(files.size() ==path.size()) {
callback.fileCallback(files);
count=0;
files.clear();
}else{
try{
CompressorImage1(count,context,path,callback);
}catch(IOException e) {
e.printStackTrace();
}
} }
@Override
public voidonError(Throwable e) {
}
}).launch();//启动压缩
/* Luban.get(context)
.load(actualImage) //传人要压缩的图片
.putGear(3) //设定压缩档次,默认三挡(据说最接近朋友圈??)
.setCompressListener(new OnCompressListener() {
@Override
public void onStart() {
}
@Override
public void onSuccess(File file) {
Log.e("TAG", "压缩第几张: " + count + "路径-------->" + file.getAbsolutePath());
files.add(file);
count++;
if (files.size() == path.size()) {
callback.fileCallback(files);
count=0;
files.clear();
} else {
CompressorImage1(count, context, path, callback);
}
}
@Override
public void onError(Throwable e) {
}
}) //设置压缩监听器
.launch(); */ //启动压缩
returnfiles;
}
public interfaceCompressCallback {
voidfileCallback(List files);
}
}