Skip to content

Commit

Permalink
v0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
郑晓勇 committed May 7, 2017
1 parent 7ef6d0e commit fa5abb9
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tiny/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
19 changes: 18 additions & 1 deletion tiny/src/main/java/com/zxy/tiny/Tiny.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -160,8 +160,25 @@ public static final class FileCompressOptions extends BitmapCompressOptions {
* The output path of the compressed file.
* <p>
* By default,we will according to time to generate a outfile.
* <p>
* 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.
* <p>
* By default,we will according to time to generate a outfile.
*/
public String[] outfiles;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}

}

0 comments on commit fa5abb9

Please sign in to comment.