فهرست منبع

solve issues like #187

郑梓斌 6 سال پیش
والد
کامیت
555eb9dec7

+ 27 - 7
example/src/main/java/top/zibin/luban/example/MainActivity.java

@@ -8,10 +8,14 @@ import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.text.TextUtils;
+import android.util.Log;
 import android.view.View;
 import android.widget.Button;
 
 import java.io.File;
+import java.math.BigInteger;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
@@ -27,6 +31,7 @@ import me.iwf.photopicker.PhotoPicker;
 import top.zibin.luban.CompressionPredicate;
 import top.zibin.luban.Luban;
 import top.zibin.luban.OnCompressListener;
+import top.zibin.luban.OnRenameListener;
 
 public class MainActivity extends AppCompatActivity {
   private static final String TAG = "Luban";
@@ -75,8 +80,8 @@ public class MainActivity extends AppCompatActivity {
         mImageList.clear();
 
         ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
-//        compressWithLs(photos);
-        compressWithRx(photos);
+        compressWithLs(photos);
+//        compressWithRx(photos);
       }
     }
   }
@@ -87,7 +92,9 @@ public class MainActivity extends AppCompatActivity {
         .map(new Function<List<String>, List<File>>() {
           @Override
           public List<File> apply(@NonNull List<String> list) throws Exception {
-            return Luban.with(MainActivity.this).load(list).get();
+            return Luban.with(MainActivity.this)
+                .load(list)
+                .get();
           }
         })
         .observeOn(AndroidSchedulers.mainThread())
@@ -95,6 +102,7 @@ public class MainActivity extends AppCompatActivity {
           @Override
           public void accept(@NonNull List<File> list) throws Exception {
             for (File file : list) {
+              Log.i(TAG, file.getAbsolutePath());
               showResult(photos, file);
             }
           }
@@ -115,19 +123,31 @@ public class MainActivity extends AppCompatActivity {
             return !(TextUtils.isEmpty(path) || path.toLowerCase().endsWith(".gif"));
           }
         })
-        .setCompressListener(new OnCompressListener() {
+        .setRenameListener(new OnRenameListener() {
           @Override
-          public void onStart() {
+          public String rename(String filePath) {
+            try {
+              MessageDigest md = MessageDigest.getInstance("MD5");
+              md.update(filePath.getBytes());
+              return new BigInteger(1, md.digest()).toString(32);
+            } catch (NoSuchAlgorithmException e) {
+              e.printStackTrace();
+            }
+            return "";
           }
+        })
+        .setCompressListener(new OnCompressListener() {
+          @Override
+          public void onStart() { }
 
           @Override
           public void onSuccess(File file) {
+            Log.i(TAG, file.getAbsolutePath());
             showResult(photos, file);
           }
 
           @Override
-          public void onError(Throwable e) {
-          }
+          public void onError(Throwable e) { }
         }).launch();
   }
 

+ 1 - 1
library/src/main/java/top/zibin/luban/Checker.java

@@ -51,7 +51,7 @@ enum Checker {
     return path.substring(path.lastIndexOf("."), path.length());
   }
 
-  boolean isNeedCompress(int leastCompressSize, String path) {
+  boolean needCompress(int leastCompressSize, String path) {
     if (leastCompressSize > 0) {
       File source = new File(path);
       return source.exists() && source.length() > (leastCompressSize << 10);

+ 2 - 1
library/src/main/java/top/zibin/luban/InputStreamProvider.java

@@ -5,10 +5,11 @@ import java.io.InputStream;
 
 /**
  * 通过此接口获取输入流,以兼容文件、FileProvider方式获取到的图片
+ * <p>
  * Get the input stream through this interface, and obtain the picture using compatible files and FileProvider
- * Created by MrFeng on 2018/4/23.
  */
 public interface InputStreamProvider {
+
   InputStream open() throws IOException;
 
   String getPath();

+ 47 - 25
library/src/main/java/top/zibin/luban/Luban.java

@@ -31,6 +31,7 @@ public class Luban implements Handler.Callback {
 
   private String mTargetDir;
   private int mLeastCompressSize;
+  private OnRenameListener mRenameListener;
   private OnCompressListener mCompressListener;
   private CompressionPredicate mCompressionPredicate;
   private List<InputStreamProvider> mStreamProviders;
@@ -39,6 +40,7 @@ public class Luban implements Handler.Callback {
 
   private Luban(Builder builder) {
     this.mTargetDir = builder.mTargetDir;
+    this.mRenameListener = builder.mRenameListener;
     this.mStreamProviders = builder.mStreamProviders;
     this.mCompressListener = builder.mCompressListener;
     this.mLeastCompressSize = builder.mLeastCompressSize;
@@ -51,7 +53,7 @@ public class Luban implements Handler.Callback {
   }
 
   /**
-   * Returns a mFile with a cache audio name in the private cache directory.
+   * Returns a file with a cache image name in the private cache directory.
    *
    * @param context A context.
    */
@@ -68,6 +70,16 @@ public class Luban implements Handler.Callback {
     return new File(cacheBuilder);
   }
 
+  private File getImageCustomFile(Context context, String filename) {
+    if (TextUtils.isEmpty(mTargetDir)) {
+      mTargetDir = getImageCacheDir(context).getAbsolutePath();
+    }
+
+    String cacheBuilder = mTargetDir + "/" + filename;
+
+    return new File(cacheBuilder);
+  }
+
   /**
    * Returns a directory with a default name in the private cache directory of the application to
    * use to store retrieved audio.
@@ -125,20 +137,7 @@ public class Luban implements Handler.Callback {
           try {
             mHandler.sendMessage(mHandler.obtainMessage(MSG_COMPRESS_START));
 
-            File result;
-            if (mCompressionPredicate != null) {
-              if (mCompressionPredicate.apply(path.getPath())) {
-                result = Checker.SINGLE.isNeedCompress(mLeastCompressSize, path.getPath()) ?
-                    new Engine(path, getImageCacheFile(context, Checker.SINGLE.extSuffix(path.getPath()))).compress() :
-                    new File(path.getPath());
-              } else {
-                result = new File(path.getPath());
-              }
-            } else {
-              result = Checker.SINGLE.isNeedCompress(mLeastCompressSize, path.getPath()) ?
-                  new Engine(path, getImageCacheFile(context, Checker.SINGLE.extSuffix(path.getPath()))).compress() :
-                  new File(path.getPath());
-            }
+            File result = compress(context, path);
 
             mHandler.sendMessage(mHandler.obtainMessage(MSG_COMPRESS_SUCCESS, result));
           } catch (IOException e) {
@@ -165,20 +164,37 @@ public class Luban implements Handler.Callback {
     Iterator<InputStreamProvider> iterator = mStreamProviders.iterator();
 
     while (iterator.hasNext()) {
-      InputStreamProvider path = iterator.next();
+      results.add(compress(context, iterator.next()));
+      iterator.remove();
+    }
+
+    return results;
+  }
 
-      if (mCompressionPredicate != null) {
-        results.add(mCompressionPredicate.apply(path.getPath()) ?
-            new Engine(path, getImageCacheFile(context, Checker.SINGLE.extSuffix(path.getPath()))).compress() :
-            new File(path.getPath()));
+  private File compress(Context context, InputStreamProvider path) throws IOException {
+    File result;
+
+    File outFile = getImageCacheFile(context, Checker.SINGLE.extSuffix(path.getPath()));
+
+    if (mRenameListener != null) {
+      String filename = mRenameListener.rename(path.getPath());
+      outFile = getImageCustomFile(context, filename);
+    }
+
+    if (mCompressionPredicate != null) {
+      if (mCompressionPredicate.apply(path.getPath())
+          && Checker.SINGLE.needCompress(mLeastCompressSize, path.getPath())) {
+        result = new Engine(path, outFile).compress();
       } else {
-        results.add(new Engine(path, getImageCacheFile(context, Checker.SINGLE.extSuffix(path.getPath()))).compress());
+        result = new File(path.getPath());
       }
-
-      iterator.remove();
+    } else {
+      result = Checker.SINGLE.needCompress(mLeastCompressSize, path.getPath()) ?
+          new Engine(path, outFile).compress() :
+          new File(path.getPath());
     }
 
-    return results;
+    return result;
   }
 
   @Override
@@ -202,10 +218,11 @@ public class Luban implements Handler.Callback {
   public static class Builder {
     private Context context;
     private String mTargetDir;
-    private List<InputStreamProvider> mStreamProviders;
     private int mLeastCompressSize = 100;
+    private OnRenameListener mRenameListener;
     private OnCompressListener mCompressListener;
     private CompressionPredicate mCompressionPredicate;
+    private List<InputStreamProvider> mStreamProviders;
 
     Builder(Context context) {
       this.context = context;
@@ -277,6 +294,11 @@ public class Luban implements Handler.Callback {
       return this;
     }
 
+    public Builder setRenameListener(OnRenameListener listener) {
+      this.mRenameListener = listener;
+      return this;
+    }
+
     public Builder setCompressListener(OnCompressListener listener) {
       this.mCompressListener = listener;
       return this;

+ 22 - 0
library/src/main/java/top/zibin/luban/OnRenameListener.java

@@ -0,0 +1,22 @@
+package top.zibin.luban;
+
+/**
+ * Author: zibin
+ * Datetime: 2018/5/18
+ * <p>
+ * 提供修改压缩图片命名接口
+ * <p>
+ * A functional interface (callback) that used to rename the file after compress.
+ */
+public interface OnRenameListener {
+
+  /**
+   * 压缩前调用该方法用于修改压缩后文件名
+   * <p>
+   * Call before compression begins.
+   *
+   * @param filePath 传入文件路径/ file path
+   * @return 返回重命名后的字符串/ file name
+   */
+  String rename(String filePath);
+}