diff --git a/example/src/main/java/top/zibin/luban/example/MainActivity.java b/example/src/main/java/top/zibin/luban/example/MainActivity.java index f2cba1f..751f916 100644 --- a/example/src/main/java/top/zibin/luban/example/MainActivity.java +++ b/example/src/main/java/top/zibin/luban/example/MainActivity.java @@ -170,8 +170,10 @@ public void accept(@NonNull List list) { })); } + private Luban mLuban;//Rx不在意了 private void withLs(final List photos) { - Luban.with(this) + final int[] i = {0}; + mLuban = Luban.with(this) .load(photos) .ignoreBy(100) .setTargetDir(getPath()) @@ -197,12 +199,19 @@ public String rename(String filePath) { }) .setCompressListener(new OnCompressListener() { @Override - public void onStart() { } + public void onStart() { + if(i[0] ==0){ + //这次压缩完了就不再压缩,释放资源 + mLuban.setContinue(false); + } + } @Override public void onSuccess(File file) { Log.i(TAG, file.getAbsolutePath()); + Log.i(TAG, "compress img :"+i[0]); showResult(originPhotos, file); + i[0]++; } @Override diff --git a/library/src/main/java/top/zibin/luban/Luban.java b/library/src/main/java/top/zibin/luban/Luban.java index 758d4ab..388dd13 100644 --- a/library/src/main/java/top/zibin/luban/Luban.java +++ b/library/src/main/java/top/zibin/luban/Luban.java @@ -116,6 +116,7 @@ private static File getImageCacheDir(Context context, String cacheName) { /** * start asynchronous compress thread */ + private boolean mIsContinue = true; private void launch(final Context context) { if (mStreamProviders == null || mStreamProviders.size() == 0 && mCompressListener != null) { mCompressListener.onError(new NullPointerException("image file cannot be null")); @@ -130,6 +131,9 @@ private void launch(final Context context) { @Override public void run() { try { + if(!mIsContinue){//线程空转,资源释放 + return; + } mHandler.sendMessage(mHandler.obtainMessage(MSG_COMPRESS_START)); File result = compress(context, path); @@ -145,6 +149,10 @@ public void run() { } } + public void setContinue(boolean isContinue){ + mIsContinue = isContinue; + } + /** * start compress and return the file */ @@ -229,6 +237,7 @@ public static class Builder { private OnCompressListener mCompressListener; private CompressionPredicate mCompressionPredicate; private List mStreamProviders; + private Luban mLuban; Builder(Context context) { this.context = context; @@ -236,7 +245,7 @@ public static class Builder { } private Luban build() { - return new Luban(this); + return mLuban = new Luban(this); } public Builder load(InputStreamProvider inputStreamProvider) { @@ -358,8 +367,9 @@ public Builder filter(CompressionPredicate compressionPredicate) { /** * begin compress image with asynchronous */ - public void launch() { + public Luban launch() { build().launch(context); + return mLuban; } public File get(final String path) throws IOException {