/**
-
使用鲁班RxJava模式压缩
-
@param path
*/
private void initLuBanRxJava(String path) {
final String newPath = FileUtils.createFile(this.getApplicationContext());
final List photos = new ArrayList<>();
photos.add(path);
photos.add(path);
photos.add(path);
Flowable.just(photos)//注意,可以单个压缩,也可以list压缩
.subscribeOn(Schedulers.io())
.map(new Function<List, List>() {
@Override
public List apply(@io.reactivex.annotations.NonNull List strings) throws Exception {
/**
- 如果需要保存到本地就使用setTargetDir方法
*/
return Luban.with(CompressorActivity.this).load(photos).setTargetDir(newPath).get();
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List>() {
@Override
public void accept(List files) throws Exception {
Glide.with(CompressorActivity.this)
.load(files.get(0))
.into(mImageNew);
mText.setText(“压缩后大小” + FileUtils.getDataSize(files.get(0).length()));
}
});
}
这里我使用的是一张13.29M的图片,使用LuBan压缩后得到的图片大小为120.73KB,压缩可谓是高的。
Compressor:
LuBan压缩后获得图片返回结果一般为File类型,而Compressor可以指定返回结果为Bitmap,同时Compressor还可以自定义压缩尺寸,这样对开发者来说,相对比较~~~
- 引入Compressor
compile ‘id.zelory:compressor:2.1.0’
- 使用Compressor IO模式压缩返回File
/**
- 使用Compressor IO模式压缩返回File
*/
private void initCompressorIOFile(String path) {
try {
File file = new Compressor(this).compressToFile(new File(path));
Glide.with(CompressorActivity.this)
.load(file)
.into(mImageNew);
mText.setText(“压缩后大小” + FileUtils.getDataSize(file.length()));//170.12KB
} catch (IOException e) {
e.printStackTrace();
}
}
- 使用Compressor IO模式压缩返回BitMap
/**
- 使用Compressor IO模式压缩返回BitMap
*/
private void initCompressorIOBitMap(String path) {
try {
Bitmap bitmap = new Compressor(this).compressToBitmap(new File(path));
mImageNew.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
- 使用Compressor IO模式自定义压缩
/**
-
使用Compressor IO模式自定义压缩
-
@param path
*/
private void initCompressorIO(String path) {
try {
File file = new Compressor(this)
.setMaxWidth(640)
.setMaxHeight(480)
.setQuality(75)
.setCompressFormat(Bitmap.CompressFormat.WEBP)
.setDestinationDirectoryPath(FileUtils.createFile(this))
.compressToFile(new File(path));
Glide.with(CompressorActivity.this)
.load(file)
.into(mImageNew);
mText.setText(“压缩后大小” + FileUtils.getDataSize(file.length()));//32.64KB
} catch (IOException e) {
e.printStackTrace();
}
}
- 使用Compressor RxJava模式压缩
首先需要引入RxJava和RxAndroid
compile ‘io.reactivex.rxjava2:rxjava:2.1.7’
compile ‘io.reactivex.rxjava2:rxandroid:2.0.1’
/**
-
使用Compressor RxJava模式压缩
-
@param path
*/
private void initCompressorRxJava(String path) {
new Compressor(this)
.compressToFileAsFlowable(new File(path))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(File file) {
Glide.with(CompressorActivity.this)
.load(file)
.into(mImageNew);
mText.setText(“压缩后大小” + FileUtils.getDataSize(file.length()));//170.27KB
}
}, new Consumer() {
@Override
public void accept(Throwable throwable) {
throwable.printStackTrace();
mText.setText(“压缩失败了”);
}
});
}
总结:
1、LuBan的优点:LuBan压缩方法在默认的情况下,压缩比要比Compressor要高,同时压缩的对象丰富,(包括file,path,List),可以传入多张图片集合直接进行压缩等。
2、Compressor的优点,返回的对象相对丰富,可以返回File或者BitMap,同时Compressor可以自定义压缩比例,定制化比较高等。
LuBan的gitHub地址:https://github.com/Curzibn/Luban
Compressor的gitHub地址:https://github.com/zetbaitsu/Compressor
整个测试源码:
package tsou.cn.lubancompressor;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import id.zelory.compressor.Compressor;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;
public class CompressorActivity extends AppCompatActivity {
private ImageView mImageOld;
private ImageView mImageNew;
private static final String TYPE = “type”;
public static final int REQUEST_PICK_IMAGE = 11101;
String[] mPermissionList = new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE};
private int type;
/**
- LuBanCompressor
*/
private TextView mText;
public static void actionStart(Context context, int type) {
Intent intent = new Intent(context, CompressorActivity.class);
intent.putExtra(TYPE, type);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compressor);
initData();
initView();
ActivityCompat.requestPermissions(CompressorActivity.this, mPermissionList, 100);
}
private void initData() {
type = getIntent().getIntExtra(TYPE, -1);
}
private void initView() {
mImageOld = (ImageView) findViewById(R.id.image_old);
mImageNew = (ImageView) findViewById(R.id.image_new);
mText = (TextView) findViewById(R.id.text);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 100:
boolean writeExternalStorage = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean readExternalStorage = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (grantResults.length > 0 && writeExternalStorage && readExternalStorage) {
getImage();
} else {
Toast.makeText(this, “请设置必要权限”, Toast.LENGTH_SHORT).show();
}
break;
}
}
private void getImage() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType(“image/*”),
REQUEST_PICK_IMAGE);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(“image/*”);
startActivityForResult(intent, REQUEST_PICK_IMAGE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case REQUEST_PICK_IMAGE:
if (data != null) {
compressImage(RealPathFromUriUtils.getRealPathFromUri(this, data.getData()));
} else {
Toast.makeText(this, “图片损坏,请重新选择”, Toast.LENGTH_SHORT).show();
}
break;
}
}
}
private void compressImage(String path) {
switch (type) {
case 0:
initLuBanIO(path);
break;
case 1:
initLuBanRxJava(path);
break;
case 2:
initCompressorIOFile(path);
break;
case 3:
initCompressorIOBitMap(path);
break;
case 4:
initCompressorIO(path);
break;
case 5:
initCompressorRxJava(path);
break;
}
}
/**
-
使用鲁班IO模式压缩
-
@param path
*/
private void initLuBanIO(String path) {
final String newPath = FileUtils.createFile(this.getApplicationContext());
Glide.with(this)
.load(path)
.into(mImageOld);
Luban.with(this)
.load(path) // 传人要压缩的图片列表(包括file,path,List)
.ignoreBy(100) // 忽略不压缩图片的大小
.setTargetDir(newPath)
.setCompressListener(new OnCompressListener() {
@Override
public void onStart() {
// TODO 压缩开始前调用,可以在方法内启动 loading UI
Log.d(“huangxiaoguo”, “使用鲁班IO模式压缩,开始压缩”);
}
@Override
public void onSuccess(File file) {
// TODO 压缩成功后调用,返回压缩后的图片文件
Log.d(“huangxiaoguo”, “使用鲁班IO模式压缩,压缩成功”);
Glide.with(CompressorActivity.this)
.load(file)
.into(mImageNew);
mText.setText(“压缩后大小” + FileUtils.getDataSize(file.length()));
}
@Override
public void onError(Throwable e) {
// TODO 当压缩过程出现问题时调用
Log.e(“huangxiaoguo”, “使用鲁班IO模式压缩,压缩过程出现问题” + e.getMessage());
}
}).launch(); //启动压缩
}
/**
-
使用鲁班RxJava模式压缩
-
@param path
*/
private void initLuBanRxJava(String path) {
final String newPath = FileUtils.createFile(this.getApplicationContext());
final List photos = new ArrayList<>();
photos.add(path);
photos.add(path);
photos.add(path);
Flowable.just(photos)//注意,可以单个压缩,也可以list压缩
.subscribeOn(Schedulers.io())
.map(new Function<List, List>() {
@Override
public List apply(@io.reactivex.annotations.NonNull List strings) throws Exception {
/**
- 如果需要保存到本地就使用setTargetDir方法
*/
return Luban.with(CompressorActivity.this).load(photos).setTargetDir(newPath).get();
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List>() {
@Override
public void accept(List files) throws Exception {
Glide.with(CompressorActivity.this)
.load(files.get(0))
.into(mImageNew);
mText.setText(“压缩后大小” + FileUtils.getDataSize(files.get(0).length()));
}
});
}
/**
- 使用Compressor IO模式压缩返回File
*/
private void initCompressorIOFile(String path) {
try {
File file = new Compressor(this).compressToFile(new File(path));
Glide.with(CompressorActivity.this)
.load(file)
.into(mImageNew);
mText.setText(“压缩后大小” + FileUtils.getDataSize(file.length()));//170.12KB
} catch (IOException e) {
e.printStackTrace();
}
}
/**
- 使用Compressor IO模式压缩返回BitMap
*/
private void initCompressorIOBitMap(String path) {
try {
Bitmap bitmap = new Compressor(this).compressToBitmap(new File(path));
mImageNew.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
-
使用Compressor IO模式自定义压缩
-
@param path
*/
private void initCompressorIO(String path) {
try {
File file = new Compressor(this)
.setMaxWidth(640)
.setMaxHeight(480)
.setQuality(75)
.setCompressFormat(Bitmap.CompressFormat.WEBP)
.setDestinationDirectoryPath(FileUtils.createFile(this))
.compressToFile(new File(path));
Glide.with(CompressorActivity.this)
.load(file)
.into(mImageNew);
mText.setText(“压缩后大小” + FileUtils.getDataSize(file.length()));//32.64KB
} catch (IOException e) {
e.printStackTrace();
}
}
/**
-
使用Compressor RxJava模式压缩
-
@param path
*/
private void initCompressorRxJava(String path) {
new Compressor(this)
.compressToFileAsFlowable(new File(path))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(File file) {
Glide.with(CompressorActivity.this)
.load(file)
.into(mImageNew);
mText.setText(“压缩后大小” + FileUtils.getDataSize(file.length()));//170.27KB
}
}, new Consumer() {
@Override
public void accept(Throwable throwable) {
throwable.printStackTrace();
mText.setText(“压缩失败了”);
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
写在最后
本次我的分享也接近尾声了,感谢你们在百忙中花上一下午来这里聆听我的宣讲,希望在接下来的日子,我们共同成长,一起进步!!!
最后放上一个大概的Android学习方向及思路(详细的内容太多了~),提供给大家:
对于程序员来说,要学习的知识内容、技术有太多太多,这里就先放上一部分,其他的内容有机会在后面的文章向大家呈现出来,不过我自己所有的学习资料都整理成了一个文档,一直在不断学习,希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!
为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!希望读到这的您能点个小赞和关注下我,以后还会更新技术干货,谢谢您的支持!
Android架构师之路很漫长,一起共勉吧!
如果你觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言,一定会认真查询,修正不足,谢谢。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
puvCthM6-1713017764854)]
[外链图片转存中…(img-rPIr8VyU-1713017764855)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
写在最后
本次我的分享也接近尾声了,感谢你们在百忙中花上一下午来这里聆听我的宣讲,希望在接下来的日子,我们共同成长,一起进步!!!
最后放上一个大概的Android学习方向及思路(详细的内容太多了~),提供给大家:
[外链图片转存中…(img-cfiiUzt3-1713017764855)]
对于程序员来说,要学习的知识内容、技术有太多太多,这里就先放上一部分,其他的内容有机会在后面的文章向大家呈现出来,不过我自己所有的学习资料都整理成了一个文档,一直在不断学习,希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!
为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!希望读到这的您能点个小赞和关注下我,以后还会更新技术干货,谢谢您的支持!
Android架构师之路很漫长,一起共勉吧!
如果你觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言,一定会认真查询,修正不足,谢谢。
[外链图片转存中…(img-eBvYMaZB-1713017764855)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!