11package Android .Zone .Image ;
22
3+ import java .io .ByteArrayInputStream ;
4+ import java .io .ByteArrayOutputStream ;
35import java .io .File ;
6+ import java .io .FileInputStream ;
7+ import java .io .FileNotFoundException ;
48import java .io .IOException ;
9+ import java .io .InputStream ;
10+ import java .io .OutputStream ;
511
612import android .content .Context ;
713import android .graphics .Bitmap ;
814import android .graphics .BitmapFactory ;
915import android .util .Log ;
16+ import Android .Zone .SD .FileUtils_SD ;
1017import Android .Zone .SD .SdSituation ;
1118import Android .Zone .Utils .AppUtils ;
1219import Android .Zone .Utils .MD5Utils ;
20+ import Java .Zone .CustomException .OperationFailException ;
21+ import Java .Zone .IO .IOUtils ;
22+ import Java .Zone .Utils .FileUtils ;
1323
1424public class DiskLruUtils {
1525 private static DiskLruUtils diskLru = new DiskLruUtils ();
@@ -23,6 +33,7 @@ public class DiskLruUtils {
2333
2434 private static final String TAG ="DiskLruUtils" ;
2535 private static boolean writeLog =true ;
36+ private static MemoryCache cache =new MemoryCache ();
2637 public static void log (String str ){
2738 if (writeLog ) {
2839 Log .d (TAG , str );
@@ -31,38 +42,72 @@ public static void log(String str){
3142
3243 private DiskLruUtils () {
3344 }
34- public static DiskLruUtils getInstance (){
35- return diskLru ;
36- }
3745
3846 /**
3947 * 版本号改变 则自动清除
4048 * @param context
4149 * @return
4250 */
43- public void openLru (Context context ) {
51+ public static DiskLruUtils openLru (Context context ) {
4452 try {
4553 /**
4654 * open()方法接收四个参数,第一个参数指定的是数据的缓存地址,
4755 * 第二个参数指定当前应用程序的版本号,
4856 * 第三个参数指定同一个key可以对应多少个缓存文件,基本都是传1,第四个参数指定最多可以缓存多少字节的数据。
4957 */
50- File cacheDir = SdSituation .getDiskCacheDir (context , DirName );
58+ // File cacheDir = SdSituation.getDiskCacheDir(context, DirName);
59+ File cacheDir = FileUtils_SD .FolderCreateOrGet ("Love" ,DirName );
5160 if (!cacheDir .exists ()) {
5261 cacheDir .mkdirs ();
5362 }
5463 mDiskLruCache = DiskLruCache .open (cacheDir ,AppUtils .getAppVersion (context ), 1 , CacheMax );
5564 } catch (IOException e ) {
5665 e .printStackTrace ();
5766 }
67+ return diskLru ;
5868 }
5969
6070 /**
6171 * 所以只有成功才调用这个方法
6272 *
6373 * @param url
6474 */
65- public void addUrl (String url ) {
75+ public void addUrl (String url ,String path ) {
76+ addUrl (url , Compress_Sample_Utils .getRawBitmap (path ));
77+ }
78+ private static ByteArrayInputStream bitmapToOs (Bitmap bt ){
79+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
80+ bt .compress (Bitmap .CompressFormat .JPEG , 100 , baos );// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
81+ ByteArrayInputStream isBm = new ByteArrayInputStream (baos .toByteArray ());// 把压缩后的数据baos存放到ByteArrayInputStream中
82+ return isBm ;
83+ }
84+ // -----------------------------------------------------------readToOutStream---------------------------------------------------------------
85+ private static boolean readToOutStream (Bitmap bt ,OutputStream os ){
86+ InputStream in =bitmapToOs (bt );
87+ byte [] buffer = new byte [1024 ];
88+ int len = 0 ;
89+ try {
90+ while ((len = in .read (buffer )) != -1 ) {
91+ os .write (buffer , 0 , len );
92+ }
93+ } catch (IOException e ) {
94+ throw new OperationFailException ("流读取发生IOException!!!" );
95+ } finally {
96+ try {
97+ in .close ();
98+ os .close ();
99+ } catch (IOException e ) {
100+ throw new OperationFailException ("流关闭发生异常!" );
101+ }
102+ }
103+ return true ;
104+ }
105+ /**
106+ * 所以只有成功才调用这个方法
107+ *
108+ * @param url
109+ */
110+ public void addUrl (String url , Bitmap bm ) {
66111 // if (downloadUrlToStream(imageUrl, outputStream)) {
67112 // editor.commit();
68113 // } else {
@@ -71,8 +116,13 @@ public void addUrl(String url) {
71116 String key = MD5Utils .hashKeyForDisk (url );
72117 try {
73118 DiskLruCache .Editor editor = mDiskLruCache .edit (key );
74- editor .commit ();
75- log ("addUrl:" +url );
119+ OutputStream outputStream = editor .newOutputStream (0 );
120+ if (readToOutStream (bm , outputStream )){
121+ editor .commit ();
122+ log ("addUrl:" +url );
123+ }else {
124+ editor .abort ();
125+ }
76126 } catch (IOException e ) {
77127 e .printStackTrace ();
78128 }
@@ -95,11 +145,15 @@ public Bitmap getBitmapByUrl(String url) {
95145 Bitmap bitmap = null ;
96146 try {
97147 DiskLruCache .Snapshot snapShot = mDiskLruCache .get (key );
98- bitmap = BitmapFactory .decodeStream (snapShot .getInputStream (0 ));
148+ if (snapShot != null ){
149+ bitmap = BitmapFactory .decodeStream (snapShot .getInputStream (0 ));
150+ cache .put (key , bitmap );
151+ log ("getBitmapByUrl:" +url );
152+ }
99153 } catch (IOException e ) {
100154 e .printStackTrace ();
101155 }
102- return bitmap ;
156+ return cache . get ( key ) ;
103157 }
104158
105159 /**
@@ -119,7 +173,9 @@ public void flush() {
119173 */
120174 public void close () {
121175 try {
122- mDiskLruCache .close ();
176+ if (!mDiskLruCache .isClosed ()) {
177+ mDiskLruCache .close ();
178+ }
123179 } catch (IOException e ) {
124180 e .printStackTrace ();
125181 }
@@ -129,7 +185,9 @@ public void close() {
129185 */
130186 public void delete () {
131187 try {
132- mDiskLruCache .delete ();
188+ if (!mDiskLruCache .isClosed ()) {
189+ mDiskLruCache .delete ();
190+ }
133191 } catch (IOException e ) {
134192 e .printStackTrace ();
135193 }
0 commit comments