导入依赖:

 implementation 'top.zibin:Luban:1.1.3'
public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        File file = new File("/sdcard/AAAImage/image1.png");
        Luban.with(this)
                .load(file)                                   // 传人要压缩的图片列表
                .ignoreBy(100)                                  // 忽略不压缩图片的大小
                .setTargetDir("/sdcard/AAAData")                        // 设置压缩后文件存储位置
                .setCompressListener(new OnCompressListener() { //设置回调
                    @Override
                    public void onStart() {
                        // TODO 压缩开始前调用,可以在方法内启动 loading UI
                        System.out.println("启动");
                    }

                    @Override
                    public void onSuccess(File file) {
                        // TODO 压缩成功后调用,返回压缩后的图片文件
                        System.out.println("成功");
                    }

                    @Override
                    public void onError(Throwable e) {
                        // TODO 当压缩过程出现问题时调用
                        System.out.println("出错");
                    }
                }).launch();    //启动压缩
    }
}

 

相关文章:

  • 2021-12-02
  • 2021-12-15
  • 2021-05-02
  • 2021-10-18
猜你喜欢
  • 2021-10-20
  • 2022-12-23
  • 2021-10-19
  • 2021-09-10
  • 2021-04-13
  • 2021-06-23
  • 2021-07-20
相关资源
相似解决方案