From fa5abb9f3ea2d9bb94619262f8c42ed1bdcb6597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=99=93=E5=8B=87?= Date: Sun, 7 May 2017 12:12:37 +0800 Subject: [PATCH] v0.0.4 --- tiny/build.gradle | 2 +- tiny/src/main/java/com/zxy/tiny/Tiny.java | 19 +++++- .../callable/FileCompressCallableTasks.java | 58 +++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/tiny/build.gradle b/tiny/build.gradle index edc32a0..3f09e89 100644 --- a/tiny/build.gradle +++ b/tiny/build.gradle @@ -31,7 +31,7 @@ dependencies { ext { upload_group_id = 'com.zxy.android' - upload_version = '0.0.3' + upload_version = '0.0.4' site_url = 'https://github.com/Sunzxyong/Tiny' git_url = 'https://github.com/Sunzxyong/Tiny.git' diff --git a/tiny/src/main/java/com/zxy/tiny/Tiny.java b/tiny/src/main/java/com/zxy/tiny/Tiny.java index df7d75b..e086ebd 100644 --- a/tiny/src/main/java/com/zxy/tiny/Tiny.java +++ b/tiny/src/main/java/com/zxy/tiny/Tiny.java @@ -131,7 +131,7 @@ public static class BitmapCompressOptions { public int height; } - public static final class FileCompressOptions extends BitmapCompressOptions { + public static class FileCompressOptions extends BitmapCompressOptions { /** * The compression quality,value range:0~100. @@ -160,8 +160,25 @@ public static final class FileCompressOptions extends BitmapCompressOptions { * The output path of the compressed file. *

* By default,we will according to time to generate a outfile. + *

+ * for batch see {@link BatchFileCompressOptions#outfiles}. */ public String outfile; + + /** + * Whether need to cover the source file,only to the file(file、content://、file://). + */ + public boolean overrideSource = false; + } + + public static class BatchFileCompressOptions extends FileCompressOptions { + + /** + * The output paths of the compressed file. + *

+ * By default,we will according to time to generate a outfile. + */ + public String[] outfiles; } } diff --git a/tiny/src/main/java/com/zxy/tiny/callable/FileCompressCallableTasks.java b/tiny/src/main/java/com/zxy/tiny/callable/FileCompressCallableTasks.java index 6c387c6..7994c6b 100644 --- a/tiny/src/main/java/com/zxy/tiny/callable/FileCompressCallableTasks.java +++ b/tiny/src/main/java/com/zxy/tiny/callable/FileCompressCallableTasks.java @@ -7,6 +7,7 @@ import com.zxy.tiny.common.BatchCompressResult; import com.zxy.tiny.common.CompressResult; import com.zxy.tiny.common.TinyException; +import com.zxy.tiny.common.UriUtil; import com.zxy.tiny.core.BitmapCompressor; import com.zxy.tiny.core.CompressKit; import com.zxy.tiny.core.FileCompressor; @@ -69,6 +70,8 @@ public CompressResult call() throws Exception { CompressResult result = null; FileInputStream fis = null; try { + if (mCompressOptions != null && mCompressOptions.overrideSource) + mCompressOptions.outfile = mFile.getAbsolutePath(); fis = new FileInputStream(mFile); result = FileCompressor.compress(CompressKit.transformToByteArray(fis), mCompressOptions, shouldReturnBitmap, true); } catch (Exception e) { @@ -96,6 +99,10 @@ public UriAsFileCallable(Tiny.FileCompressOptions options, boolean withBitmap, U @Override public CompressResult call() throws Exception { Bitmap bitmap = new BitmapCompressCallableTasks.UriAsBitmapCallable(mCompressOptions, mUri).call(); + if (mCompressOptions != null && mCompressOptions.overrideSource && + (UriUtil.isLocalContentUri(mUri) || UriUtil.isLocalFileUri(mUri))) { + mCompressOptions.outfile = UriUtil.getRealPathFromUri(mUri); + } return FileCompressor.compress(bitmap, mCompressOptions, shouldReturnBitmap, true); } } @@ -144,6 +151,8 @@ public BatchCompressResult call() throws Exception { BatchCompressResult result = new BatchCompressResult(); result.results = new CompressResult[mFiles.length]; + String[] outfilePaths = getBatchOutfilePaths(mCompressOptions, mFiles.length); + for (int i = 0; i < mFiles.length; i++) { File file = mFiles[i]; if (file == null) { @@ -153,6 +162,13 @@ public BatchCompressResult call() throws Exception { CompressResult compressResult = null; FileInputStream fis = null; try { + if (mCompressOptions != null) { + if (outfilePaths != null && outfilePaths.length == mFiles.length) + mCompressOptions.outfile = outfilePaths[i]; + + if (mCompressOptions.overrideSource) + mCompressOptions.outfile = file.getAbsolutePath(); + } fis = new FileInputStream(file); compressResult = FileCompressor.compress(CompressKit.transformToByteArray(fis), mCompressOptions, shouldReturnBitmap, true); } catch (Exception e) { @@ -189,8 +205,13 @@ public BatchCompressResult call() throws Exception { BatchCompressResult result = new BatchCompressResult(); result.results = new CompressResult[mBitmaps.length]; + String[] outfilePaths = getBatchOutfilePaths(mCompressOptions, mBitmaps.length); + for (int i = 0; i < mBitmaps.length; i++) { Bitmap bitmap = mBitmaps[i]; + if (mCompressOptions != null && outfilePaths != null && outfilePaths.length == mBitmaps.length) + mCompressOptions.outfile = outfilePaths[i]; + CompressResult compressResult = FileCompressor.compress(bitmap, mCompressOptions, shouldReturnBitmap, false); if (compressResult != null) result.success = true; @@ -215,12 +236,17 @@ public BatchCompressResult call() throws Exception { BatchCompressResult result = new BatchCompressResult(); result.results = new CompressResult[mUris.length]; + String[] outfilePaths = getBatchOutfilePaths(mCompressOptions, mUris.length); + for (int i = 0; i < mUris.length; i++) { Uri uri = mUris[i]; if (uri == null) { result.results[i] = null; continue; } + if (mCompressOptions != null && outfilePaths != null && outfilePaths.length == mUris.length) + mCompressOptions.outfile = outfilePaths[i]; + CompressResult compressResult = new UriAsFileCallable(mCompressOptions, shouldReturnBitmap, uri).call(); if (compressResult != null) result.success = true; @@ -246,8 +272,13 @@ public BatchCompressResult call() throws Exception { BatchCompressResult result = new BatchCompressResult(); result.results = new CompressResult[mResIds.length]; + String[] outfilePaths = getBatchOutfilePaths(mCompressOptions, mResIds.length); + for (int i = 0; i < mResIds.length; i++) { Bitmap bitmap = compress(mResIds[i], mCompressOptions, false); + if (mCompressOptions != null && outfilePaths != null && outfilePaths.length == mResIds.length) + mCompressOptions.outfile = outfilePaths[i]; + CompressResult compressResult = FileCompressor.compress(bitmap, mCompressOptions, shouldReturnBitmap, true); if (compressResult != null) result.success = true; @@ -257,4 +288,31 @@ public BatchCompressResult call() throws Exception { } } + private static String[] getBatchOutfilePaths(Tiny.FileCompressOptions options, int length) { + if (options == null || length <= 0) + return null; + String[] outfilePaths = null; + if (options instanceof Tiny.BatchFileCompressOptions) { + Tiny.BatchFileCompressOptions compressOptions = (Tiny.BatchFileCompressOptions) options; + String[] outfiles = compressOptions.outfiles; + if (outfiles != null && outfiles.length > 0) { + outfilePaths = new String[length]; + if (outfiles.length >= length) { + System.arraycopy(outfiles, 0, outfilePaths, 0, length); + } else { + for (int i = 0; i < length; i++) { + try { + outfilePaths[i] = outfiles[i]; + } catch (Exception e) { + outfilePaths[i] = null; + } + } + } + } + } else { + options.outfile = null; + } + return outfilePaths; + } + }