詹子聪 5 лет назад
Родитель
Сommit
165a56ae6d
1 измененных файлов с 19 добавлено и 29 удалено
  1. 19 29
      app/src/main/java/com/miekir/ocr/ui/OperationPresenter.java

+ 19 - 29
app/src/main/java/com/miekir/ocr/ui/OperationPresenter.java

@@ -32,7 +32,6 @@ import io.reactivex.ObservableOnSubscribe;
 import io.reactivex.android.schedulers.AndroidSchedulers;
 import io.reactivex.schedulers.Schedulers;
 import top.zibin.luban.Luban;
-import top.zibin.luban.OnCompressListener;
 
 import static android.os.Environment.DIRECTORY_PICTURES;
 
@@ -395,39 +394,30 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
                 croppedBmp.recycle();
                 rotatedBitmap.recycle();
                 if (saveCropSuccess) {
-                    Luban.with(context)
+
+                    List<File> fileList = Luban.with(context)
                             .load(file)
                             .ignoreBy(400)
-                            .setTargetDir(file.getParentFile().getAbsolutePath())
-                            .setCompressListener(new OnCompressListener() {
-                        @Override
-                        public void onStart() {
-                            // TODO 压缩开始前调用,可以在方法内启动 loading UI
-                        }
-
-                        @Override
-                        public void onSuccess(File compressedFile) {
-                            // 通知相册去获取
-                            MediaScannerConnection.scanFile(context, new String[] { compressedFile.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
-                            //MediaScannerConnection.scanFile(context, new String[] { file.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
-                            // 保存最终截图成功,根据URI获取图片的base64字符串
-                            String photoBase64 = Base64Tool.getBase64FromFilePath(compressedFile.getAbsolutePath());
-                            // 文件名相同的话,表示没有压缩
-                            if (!TextUtils.equals(file.getAbsolutePath(), compressedFile.getAbsolutePath())) {
-                                file.delete();
-                            }
-                            if (TextUtils.isEmpty(photoBase64)) {
-                                emitter.onError(new Exception("Empty image"));
-                            } else {
-                                emitter.onNext(photoBase64);
-                            }
+                            .setTargetDir(file.getParentFile().getAbsolutePath()).get();
+                    if (fileList.size() == 1) {
+                        File compressedFile = fileList.get(0);
+                        // 通知相册去获取
+                        MediaScannerConnection.scanFile(context, new String[] { compressedFile.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
+                        //MediaScannerConnection.scanFile(context, new String[] { file.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
+                        // 保存最终截图成功,根据URI获取图片的base64字符串
+                        String photoBase64 = Base64Tool.getBase64FromFilePath(compressedFile.getAbsolutePath());
+                        // 文件名相同的话,表示没有压缩
+                        if (!TextUtils.equals(file.getAbsolutePath(), compressedFile.getAbsolutePath())) {
+                            file.delete();
                         }
-
-                        @Override
-                        public void onError(Throwable e) {
+                        if (TextUtils.isEmpty(photoBase64)) {
                             emitter.onError(new Exception("Empty image"));
+                        } else {
+                            emitter.onNext(photoBase64);
                         }
-                    }).launch();
+                    } else {
+                        emitter.onError(new Exception("Empty image"));
+                    }
                 } else {
                     emitter.onError(new Exception("Empty image"));
                 }