Skip to content

Commit 12d54f2

Browse files
committed
修复BitmapUtils下载错误文件后,一直使用错误文件的问题.
1 parent 42b45f1 commit 12d54f2

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ public Bitmap downloadBitmap(String uri, BitmapDisplayConfig config, final Bitma
138138

139139
try {
140140

141-
// download to disk
141+
Bitmap bitmap = null;
142+
// try download to disk
142143
if (globalConfig.isDiskCacheEnabled()) {
143144
synchronized (mDiskCacheLock) {
144145
// Wait for disk cache to initialize
@@ -169,6 +170,11 @@ public Bitmap downloadBitmap(String uri, BitmapDisplayConfig config, final Bitma
169170
}
170171
if (snapshot != null) {
171172
bitmapMeta.inputStream = snapshot.getInputStream(DISK_CACHE_INDEX);
173+
bitmap = decodeBitmapMeta(bitmapMeta, config);
174+
if (bitmap == null) {
175+
bitmapMeta.inputStream = null;
176+
mDiskLruCache.remove(uri);
177+
}
172178
}
173179
} catch (Throwable e) {
174180
LogUtils.e(e.getMessage(), e);
@@ -177,20 +183,22 @@ public Bitmap downloadBitmap(String uri, BitmapDisplayConfig config, final Bitma
177183
}
178184
}
179185

180-
// download to memory stream
181-
if (!globalConfig.isDiskCacheEnabled() || mDiskLruCache == null || bitmapMeta.inputStream == null) {
186+
// try download to memory stream
187+
if (bitmap == null) {
182188
outputStream = new ByteArrayOutputStream();
183189
bitmapMeta.expiryTimestamp = globalConfig.getDownloader().downloadToStream(uri, outputStream, task);
184190
if (bitmapMeta.expiryTimestamp < 0) {
185191
return null;
186192
} else {
187193
bitmapMeta.data = ((ByteArrayOutputStream) outputStream).toByteArray();
194+
bitmap = decodeBitmapMeta(bitmapMeta, config);
188195
}
189196
}
190197

191-
Bitmap bitmap = decodeBitmapMeta(bitmapMeta, config);
192-
bitmap = rotateBitmapIfNeeded(uri, config, bitmap);
193-
addBitmapToMemoryCache(uri, config, bitmap, bitmapMeta.expiryTimestamp);
198+
if (bitmap != null) {
199+
bitmap = rotateBitmapIfNeeded(uri, config, bitmap);
200+
addBitmapToMemoryCache(uri, config, bitmap, bitmapMeta.expiryTimestamp);
201+
}
194202
return bitmap;
195203
} catch (Throwable e) {
196204
LogUtils.e(e.getMessage(), e);

0 commit comments

Comments
 (0)