Skip to content

Commit 26c783f

Browse files
committed
修复移除可能导致图片磁盘缓存初始化失败的问题
1 parent a9b71b7 commit 26c783f

File tree

3 files changed

+30
-38
lines changed

3 files changed

+30
-38
lines changed

library/src/com/lidroid/xutils/BitmapUtils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ public <T extends View> void display(T container, String uri, BitmapDisplayConfi
218218
return;
219219
}
220220

221+
if (TextUtils.isEmpty(uri)) {
222+
callBack.onLoadFailed(container, uri, displayConfig.getLoadFailedDrawable());
223+
return;
224+
}
225+
221226
container.clearAnimation();
222227

223228
if (callBack == null) {
@@ -234,11 +239,6 @@ public <T extends View> void display(T container, String uri, BitmapDisplayConfi
234239

235240
callBack.onPreLoad(container, uri, displayConfig);
236241

237-
if (TextUtils.isEmpty(uri)) {
238-
callBack.onLoadFailed(container, uri, displayConfig.getLoadFailedDrawable());
239-
return;
240-
}
241-
242242
// find bitmap from mem cache.
243243
Bitmap bitmap = globalConfig.getBitmapCache().getBitmapFromMemCache(uri, displayConfig);
244244

library/src/com/lidroid/xutils/bitmap/BitmapGlobalConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private BitmapGlobalConfig(Context context, String diskCachePath) {
7575
initBitmapCache();
7676
}
7777

78-
public static BitmapGlobalConfig getInstance(Context context, String diskCachePath) {
78+
public synchronized static BitmapGlobalConfig getInstance(Context context, String diskCachePath) {
7979

8080
if (TextUtils.isEmpty(diskCachePath)) {
8181
diskCachePath = OtherUtils.getDiskCacheDir(context, "xBitmapCache");

library/src/com/lidroid/xutils/bitmap/core/BitmapCache.java

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public class BitmapCache {
4040
private LruMemoryCache<MemoryCacheKey, Bitmap> mMemoryCache;
4141

4242
private final Object mDiskCacheLock = new Object();
43-
private volatile boolean isDiskCacheReady = false;
4443

4544
private BitmapGlobalConfig globalConfig;
4645

@@ -90,7 +89,7 @@ protected int sizeOf(MemoryCacheKey key, Bitmap bitmap) {
9089
public void initDiskCache() {
9190
// Set up disk cache
9291
synchronized (mDiskCacheLock) {
93-
if (globalConfig.isDiskCacheEnabled()) {
92+
if (globalConfig.isDiskCacheEnabled() && (mDiskLruCache == null || mDiskLruCache.isClosed())) {
9493
File diskCacheDir = new File(globalConfig.getDiskCachePath());
9594
if (diskCacheDir.exists() || diskCacheDir.mkdirs()) {
9695
long availableSpace = OtherUtils.getAvailableSpace(diskCacheDir);
@@ -99,14 +98,13 @@ public void initDiskCache() {
9998
try {
10099
mDiskLruCache = LruDiskCache.open(diskCacheDir, 1, 1, diskCacheSize);
101100
mDiskLruCache.setFileNameGenerator(globalConfig.getFileNameGenerator());
101+
LogUtils.d("create disk cache success");
102102
} catch (Throwable e) {
103103
mDiskLruCache = null;
104-
LogUtils.e(e.getMessage(), e);
104+
LogUtils.e("create disk cache error", e);
105105
}
106106
}
107107
}
108-
isDiskCacheReady = true;
109-
mDiskCacheLock.notifyAll();
110108
}
111109
}
112110

@@ -117,14 +115,18 @@ public void setMemoryCacheSize(int maxSize) {
117115
}
118116

119117
public void setDiskCacheSize(int maxSize) {
120-
if (mDiskLruCache != null) {
121-
mDiskLruCache.setMaxSize(maxSize);
118+
synchronized (mDiskCacheLock) {
119+
if (mDiskLruCache != null) {
120+
mDiskLruCache.setMaxSize(maxSize);
121+
}
122122
}
123123
}
124124

125125
public void setDiskCacheFileNameGenerator(FileNameGenerator fileNameGenerator) {
126-
if (mDiskLruCache != null && fileNameGenerator != null) {
127-
mDiskLruCache.setFileNameGenerator(fileNameGenerator);
126+
synchronized (mDiskCacheLock) {
127+
if (mDiskLruCache != null && fileNameGenerator != null) {
128+
mDiskLruCache.setFileNameGenerator(fileNameGenerator);
129+
}
128130
}
129131
}
130132

@@ -137,17 +139,11 @@ public Bitmap downloadBitmap(String uri, BitmapDisplayConfig config, final Bitma
137139

138140
try {
139141
Bitmap bitmap = null;
142+
140143
// try download to disk
141144
if (globalConfig.isDiskCacheEnabled()) {
142-
synchronized (mDiskCacheLock) {
143-
// Wait for disk cache to initialize
144-
while (!isDiskCacheReady) {
145-
try {
146-
mDiskCacheLock.wait();
147-
} catch (Throwable e) {
148-
break;
149-
}
150-
}
145+
if (mDiskLruCache == null) {
146+
initDiskCache();
151147
}
152148

153149
if (mDiskLruCache != null) {
@@ -245,10 +241,13 @@ public Bitmap getBitmapFromMemCache(String uri, BitmapDisplayConfig config) {
245241
* @return The file if found in cache.
246242
*/
247243
public File getBitmapFileFromDiskCache(String uri) {
248-
if (mDiskLruCache != null) {
249-
return mDiskLruCache.getCacheFile(uri, DISK_CACHE_INDEX);
244+
synchronized (mDiskCacheLock) {
245+
if (mDiskLruCache != null) {
246+
return mDiskLruCache.getCacheFile(uri, DISK_CACHE_INDEX);
247+
} else {
248+
return null;
249+
}
250250
}
251-
return null;
252251
}
253252

254253
/**
@@ -260,14 +259,8 @@ public File getBitmapFileFromDiskCache(String uri) {
260259
*/
261260
public Bitmap getBitmapFromDiskCache(String uri, BitmapDisplayConfig config) {
262261
if (uri == null || !globalConfig.isDiskCacheEnabled()) return null;
263-
synchronized (mDiskCacheLock) {
264-
while (!isDiskCacheReady) {
265-
try {
266-
mDiskCacheLock.wait();
267-
} catch (Throwable e) {
268-
break;
269-
}
270-
}
262+
if (mDiskLruCache == null) {
263+
initDiskCache();
271264
}
272265
if (mDiskLruCache != null) {
273266
LruDiskCache.Snapshot snapshot = null;
@@ -315,10 +308,10 @@ public void clearMemoryCache() {
315308

316309
public void clearDiskCache() {
317310
synchronized (mDiskCacheLock) {
318-
isDiskCacheReady = false;
319311
if (mDiskLruCache != null && !mDiskLruCache.isClosed()) {
320312
try {
321313
mDiskLruCache.delete();
314+
mDiskLruCache.close();
322315
} catch (Throwable e) {
323316
LogUtils.e(e.getMessage(), e);
324317
}
@@ -377,16 +370,15 @@ public void flush() {
377370
*/
378371
public void close() {
379372
synchronized (mDiskCacheLock) {
380-
isDiskCacheReady = false;
381373
if (mDiskLruCache != null) {
382374
try {
383375
if (!mDiskLruCache.isClosed()) {
384376
mDiskLruCache.close();
385-
mDiskLruCache = null;
386377
}
387378
} catch (Throwable e) {
388379
LogUtils.e(e.getMessage(), e);
389380
}
381+
mDiskLruCache = null;
390382
}
391383
}
392384
}

0 commit comments

Comments
 (0)