11package com .jph .takephoto .compress ;
22
3+ import android .content .Context ;
34import android .graphics .Bitmap ;
45import android .graphics .Bitmap .Config ;
56import android .graphics .BitmapFactory ;
67import android .os .Handler ;
8+
9+ import com .jph .takephoto .uitl .TFileUtils ;
10+
711import java .io .ByteArrayOutputStream ;
812import java .io .File ;
913import java .io .FileNotFoundException ;
1721 */
1822public 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 */
0 commit comments