Skip to content

Commit 4abdca4

Browse files
committed
修复KJBitmap无法缓存图片的问题
1 parent 06dbd7d commit 4abdca4

14 files changed

+39
-94
lines changed

KJFrame/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="org.kymjs.kjframe"
33
android:versionCode="224"
4-
android:versionName="2.241" >
4+
android:versionName="2.242" >
55

66
<uses-sdk
77
android:minSdkVersion="11"

KJFrame/src/org/kymjs/kjframe/KJBitmap.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.kymjs.kjframe.bitmap.ImageDisplayer;
3030
import org.kymjs.kjframe.http.Cache;
3131
import org.kymjs.kjframe.http.HttpCallBack;
32+
import org.kymjs.kjframe.http.HttpConfig;
3233
import org.kymjs.kjframe.utils.DensityUtils;
3334
import org.kymjs.kjframe.utils.FileUtils;
3435
import org.kymjs.kjframe.utils.KJLoger;
@@ -374,22 +375,16 @@ private void doSuccess(View view, Bitmap bitmap, Drawable defaultImage) {
374375
* 哪条url的缓存
375376
*/
376377
public void removeCache(String url) {
377-
BitmapConfig.mCache.remove(url);
378-
}
379-
380-
/**
381-
* 已过期,请改用cleanCache()
382-
*/
383-
@Deprecated
384-
public void removeCacheAll() {
385-
cleanCache();
378+
BitmapConfig.mMemoryCache.remove(url);
379+
HttpConfig.mCache.remove(url);
386380
}
387381

388382
/**
389383
* 清空缓存
390384
*/
391385
public void cleanCache() {
392-
BitmapConfig.mCache.clear();
386+
BitmapConfig.mMemoryCache.clean();
387+
HttpConfig.mCache.clean();
393388
}
394389

395390
/**
@@ -400,7 +395,7 @@ public void cleanCache() {
400395
* @return
401396
*/
402397
public byte[] getCache(String url) {
403-
Cache cache = BitmapConfig.mCache;
398+
Cache cache = HttpConfig.mCache;
404399
cache.initialize();
405400
Cache.Entry entry = cache.get(url);
406401
if (entry != null) {

KJFrame/src/org/kymjs/kjframe/KJHttp.java

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public void doRequest(Request<?> request) {
315315
* @return
316316
*/
317317
public byte[] getCache(String url) {
318-
Cache cache = mConfig.mCache;
318+
Cache cache = HttpConfig.mCache;
319319
cache.initialize();
320320
Cache.Entry entry = cache.get(url);
321321
if (entry != null) {
@@ -373,38 +373,14 @@ public String getStringCache(String url) {
373373
* 哪条url的缓存
374374
*/
375375
public void removeCache(String url) {
376-
mConfig.mCache.remove(url);
376+
HttpConfig.mCache.remove(url);
377377
}
378378

379379
/**
380380
* 清空缓存
381381
*/
382382
public void cleanCache() {
383-
mConfig.mCache.clear();
384-
}
385-
386-
/**
387-
* 已过期,请更换为cleanCache()
388-
*/
389-
@Deprecated
390-
public void removeAllDiskCache() {
391-
cleanCache();
392-
}
393-
394-
/**
395-
* 已过期,请更换为removeCache()
396-
*/
397-
@Deprecated
398-
public void removeDiskCache(String uri, HttpParams params) {
399-
mConfig.mCache.remove(uri);
400-
}
401-
402-
/**
403-
* 已过期,请更换为getConfig()
404-
*/
405-
@Deprecated
406-
public HttpConfig getHttpConfig() {
407-
return getConfig();
383+
HttpConfig.mCache.clean();
408384
}
409385

410386
public HttpConfig getConfig() {
@@ -431,12 +407,12 @@ public void setConfig(HttpConfig config) {
431407
private void start() {
432408
stop();// 首先关闭之前的运行,不管是否存在
433409
mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue,
434-
mConfig.mCache, mConfig.mDelivery, mConfig);
410+
HttpConfig.mCache, mConfig.mDelivery, mConfig);
435411
mCacheDispatcher.start();
436412
// 构建线程池
437413
for (int i = 0; i < mTaskThreads.length; i++) {
438414
NetworkDispatcher tasker = new NetworkDispatcher(mNetworkQueue,
439-
mConfig.mNetwork, mConfig.mCache, mConfig.mDelivery);
415+
mConfig.mNetwork, HttpConfig.mCache, mConfig.mDelivery);
440416
mTaskThreads[i] = tasker;
441417
tasker.start();
442418
}

KJFrame/src/org/kymjs/kjframe/bitmap/BitmapConfig.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515
*/
1616
package org.kymjs.kjframe.bitmap;
1717

18-
import java.io.File;
19-
2018
import org.kymjs.kjframe.bitmap.ImageDisplayer.ImageCache;
21-
import org.kymjs.kjframe.http.Cache;
22-
import org.kymjs.kjframe.http.DiskCache;
23-
import org.kymjs.kjframe.utils.FileUtils;
19+
import org.kymjs.kjframe.http.HttpConfig;
2420
import org.kymjs.kjframe.utils.KJLoger;
2521

2622
/**
@@ -33,22 +29,21 @@ public class BitmapConfig {
3329

3430
public boolean isDEBUG = KJLoger.DEBUG_LOG;
3531

36-
public static String CACHEPATH = "KJLibrary/image";
32+
/** 新版本图片缓存与http缓存处于同一目录 */
33+
@Deprecated
34+
public static String CACHEPATH = HttpConfig.CACHEPATH;
3735

3836
/** 磁盘缓存大小 */
3937
public static int DISK_CACHE_SIZE = 10 * 1024 * 1024;
4038
/** 磁盘缓存器 **/
41-
public static Cache mCache;
4239
public static ImageCache mMemoryCache;
4340

4441
public int cacheTime = 1440000;
4542
// 为了防止网速很快的时候速度过快而造成先显示加载中图片,然后瞬间显示网络图片的闪烁问题
4643
public long delayTime = 100;
4744

4845
public BitmapConfig() {
49-
File folder = FileUtils.getSaveFolder(CACHEPATH);
50-
if (mCache == null) {
51-
mCache = new DiskCache(folder, DISK_CACHE_SIZE);
46+
if (mMemoryCache == null) {
5247
mMemoryCache = new BitmapMemoryCache();
5348
}
5449
}

KJFrame/src/org/kymjs/kjframe/bitmap/BitmapMemoryCache.java

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,39 +65,14 @@ protected int sizeOf(String key, Bitmap value) {
6565
};
6666
}
6767

68+
@Override
6869
public void remove(String key) {
6970
cache.remove(key);
7071
}
7172

72-
public void removeAll() {
73-
cache.removeAll();
74-
}
75-
76-
/**
77-
* 已过期,请使用putBitmap(String key, Bitmap bitmap)
78-
*
79-
* @param key
80-
* 图片的地址
81-
* @param bitmap
82-
* 要缓存的bitmap
83-
*/
84-
@Deprecated
85-
public void put(String key, Bitmap bitmap) {
86-
if (this.get(key) == null) {
87-
cache.put(key, bitmap);
88-
}
89-
}
90-
91-
/**
92-
* 已过期,请使用gutBitmap(String key)
93-
*
94-
* @param key
95-
* 图片的地址
96-
* @return
97-
*/
98-
@Deprecated
99-
public Bitmap get(String key) {
100-
return cache.get(key);
73+
@Override
74+
public void clean() {
75+
cache.clean();
10176
}
10277

10378
/**

KJFrame/src/org/kymjs/kjframe/bitmap/ImageDisplayer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,12 @@ public class ImageDisplayer {
5454
*/
5555
public ImageDisplayer(BitmapConfig bitmapConfig) {
5656
HttpConfig config = new HttpConfig();
57-
config.mCache = BitmapConfig.mCache;
5857
// 靠,在这里踩了个坑。 最初写的是Integer.MAX_VALUE,
5958
// 结果把这个值*60000转成毫秒long以后溢出了 这次我给个死的值行不行。1000天,能不能算永久了
6059
// 其实还有一种解决办法是直接在缓存读取的时候,看到是bitmap缓存不管是否失效都返回,
61-
// 但是这种不利于自定义扩展,就不用了,有兴趣的可以看CacheDispatcher的109行
60+
// 但是这种不利于自定义扩展,就不用了,有兴趣的可以看CacheDispatcher的105行
6261
// @kymjs记录于2015.4.30
63-
config.cacheTime = bitmapConfig.cacheTime;
62+
// config.cacheTime = bitmapConfig.cacheTime;
6463
mKJHttp = new KJHttp(config);
6564
mMemoryCache = BitmapConfig.mMemoryCache;
6665
mResponseDelayMs = bitmapConfig.delayTime;
@@ -115,7 +114,6 @@ public ImageBale get(String requestUrl, int maxWidth, int maxHeight,
115114

116115
Request<Bitmap> newRequest = makeImageRequest(requestUrl, maxWidth,
117116
maxHeight);
118-
newRequest.setShouldCache(false);
119117
newRequest.setConfig(mKJHttp.getConfig());
120118
mKJHttp.doRequest(newRequest);
121119
mRequestsMap.put(requestUrl,
@@ -339,6 +337,10 @@ public void cancle(String url) {
339337
public interface ImageCache {
340338
public Bitmap getBitmap(String url);
341339

340+
public void remove(String key);
341+
342+
public void clean();
343+
342344
public void putBitmap(String url, Bitmap bitmap);
343345
}
344346
}

KJFrame/src/org/kymjs/kjframe/bitmap/ImageRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Priority getPriority() {
5555

5656
@Override
5757
public String getCacheKey() {
58-
return "";
58+
return getUrl();
5959
}
6060

6161
/**

KJFrame/src/org/kymjs/kjframe/bitmap/MemoryLruCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public final V remove(K key) {
216216
/**
217217
* @author kymjs
218218
*/
219-
public final void removeAll() {
219+
public final void clean() {
220220
map.clear();
221221
size = 0;
222222
}

KJFrame/src/org/kymjs/kjframe/http/Cache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface Cache {
2929

3030
public void remove(String key);
3131

32-
public void clear();
32+
public void clean();
3333

3434
/**
3535
* 执行在线程中

KJFrame/src/org/kymjs/kjframe/http/CacheDispatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.concurrent.BlockingQueue;
1919

2020
import org.kymjs.kjframe.KJHttp;
21+
import org.kymjs.kjframe.bitmap.ImageRequest;
2122
import org.kymjs.kjframe.utils.KJLoger;
2223

2324
import android.os.Process;
@@ -100,8 +101,7 @@ public void run() {
100101
}
101102

102103
// 如果缓存过期,去网络请求,图片缓存永久有效
103-
if (entry.isExpired()) {
104-
// && !(request instanceof ImageRequest)
104+
if (entry.isExpired() && !(request instanceof ImageRequest)) {
105105
request.setCacheEntry(entry);
106106
mNetworkQueue.put(request);
107107
continue;

0 commit comments

Comments
 (0)