Skip to content

Commit 957e698

Browse files
committed
fix #55 fix#20 压缩图片不再覆盖原截图
1 parent b44c017 commit 957e698

5 files changed

Lines changed: 54 additions & 10 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.2.1'
8+
classpath 'com.android.tools.build:gradle:2.2.2'
99
// classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
1010
// classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
1111
// NOTE: Do not place your application dependencies here; they belong

takephoto_library/src/main/java/com/jph/takephoto/app/TakePhotoImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ private void takeResult(final TResult result, final String...message) {
392392
} else {
393393
if (showCompressDialog)
394394
wailLoadDialog = TUtils.showProgressDialog(contextWrap.getActivity(),contextWrap.getActivity().getResources().getString(R.string.tip_compress));
395-
new CompressImageImpl(compressConfig,result.getImages(), new CompressImage.CompressListener() {
395+
new CompressImageImpl(contextWrap.getActivity(),compressConfig,result.getImages(), new CompressImage.CompressListener() {
396396
@Override
397397
public void onCompressSuccess(ArrayList<TImage> images) {
398398
handleTakeCallBack(result);

takephoto_library/src/main/java/com/jph/takephoto/compress/CompressImageImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jph.takephoto.compress;
22

3+
import android.content.Context;
34
import android.text.TextUtils;
45
import com.jph.takephoto.model.TImage;
56
import java.io.File;
@@ -18,9 +19,8 @@ public class CompressImageImpl implements CompressImage {
1819
private CompressImageUtil compressImageUtil;
1920
private ArrayList<TImage> images;
2021
private CompressImage.CompressListener listener;
21-
22-
public CompressImageImpl(CompressConfig config, ArrayList<TImage> images, CompressImage.CompressListener listener) {
23-
compressImageUtil = new CompressImageUtil(config);
22+
public CompressImageImpl(Context context,CompressConfig config, ArrayList<TImage> images, CompressImage.CompressListener listener) {
23+
compressImageUtil = new CompressImageUtil(context,config);
2424
this.images = images;
2525
this.listener = listener;
2626
}
@@ -52,6 +52,7 @@ private void compress(final TImage image) {
5252
compressImageUtil.compress(image.getPath(), new CompressImageUtil.CompressListener() {
5353
@Override
5454
public void onCompressSuccess(String imgPath) {
55+
image.setPath(imgPath);
5556
continueCompress(image,true);
5657
}
5758

takephoto_library/src/main/java/com/jph/takephoto/compress/CompressImageUtil.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.jph.takephoto.compress;
22

3+
import android.content.Context;
34
import android.graphics.Bitmap;
45
import android.graphics.Bitmap.Config;
56
import android.graphics.BitmapFactory;
67
import android.os.Handler;
8+
9+
import com.jph.takephoto.uitl.TFileUtils;
10+
711
import java.io.ByteArrayOutputStream;
812
import java.io.File;
913
import java.io.FileNotFoundException;
@@ -17,8 +21,10 @@
1721
*/
1822
public class CompressImageUtil{
1923
private CompressConfig config;
24+
private Context context;
2025
Handler mhHandler = new Handler();
21-
public CompressImageUtil(CompressConfig config) {
26+
public CompressImageUtil(Context context,CompressConfig config) {
27+
this.context=context;
2228
this.config=config==null?CompressConfig.getDefaultConfig():config;
2329
}
2430
public void compress(String imagePath, CompressListener listener) {
@@ -63,11 +69,12 @@ public void run() {
6369
// bitmap.recycle();//回收内存中的图片
6470
// }
6571
try {
66-
FileOutputStream fos = new FileOutputStream(new File(imgPath));//将压缩后的图片保存的本地上指定路径中
72+
File thumbnailFile=getThumbnailFile(new File(imgPath));
73+
FileOutputStream fos = new FileOutputStream(thumbnailFile);//将压缩后的图片保存的本地上指定路径中
6774
fos.write(baos.toByteArray());
6875
fos.flush();
6976
fos.close();
70-
sendMsg(true, imgPath,null,listener);
77+
sendMsg(true, thumbnailFile.getPath(),null,listener);
7178
} catch (Exception e) {
7279
sendMsg(false,imgPath,"质量压缩失败",listener);
7380
e.printStackTrace();
@@ -110,8 +117,10 @@ private void compressImageByPixel(String imgPath,CompressListener listener) thro
110117
if (config.isEnableQualityCompress()){
111118
compressImageByQuality(bitmap,imgPath,listener);//压缩好比例大小后再进行质量压缩
112119
}else {
113-
bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(new File(imgPath)));
114-
listener.onCompressSuccess(imgPath);
120+
File thumbnailFile=getThumbnailFile(new File(imgPath));
121+
bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(thumbnailFile));
122+
123+
listener.onCompressSuccess(thumbnailFile.getPath());
115124
}
116125
}
117126
/**
@@ -132,6 +141,10 @@ public void run() {
132141
}
133142
});
134143
}
144+
private File getThumbnailFile(File file){
145+
if (file==null||!file.exists())return file;
146+
return TFileUtils.getPhotoCacheDir(context,file);
147+
}
135148
/**
136149
* 压缩结果监听器
137150
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.jph.takephoto.uitl;
2+
3+
import android.content.Context;
4+
import android.util.Log;
5+
6+
import java.io.File;
7+
8+
/**
9+
* Author: crazycodeboy
10+
* Date: 2016/11/5 0007 20:10
11+
* Version:4.0.0
12+
* 技术博文:http://www.devio.org/
13+
* GitHub:https://github.com/crazycodeboy
14+
* Eamil:crazycodeboy@gmail.com
15+
*/
16+
public class TFileUtils {
17+
private static final String TAG="TFileUtils";
18+
19+
public static File getPhotoCacheDir(Context context, File file) {
20+
File cacheDir = context.getCacheDir();
21+
if (cacheDir != null) {
22+
File result = new File(cacheDir, file.getName());
23+
return result;
24+
}
25+
if (Log.isLoggable(TAG, Log.ERROR)) {
26+
Log.e(TAG, "default disk cache dir is null");
27+
}
28+
return file;
29+
}
30+
}

0 commit comments

Comments
 (0)