Skip to content

Commit

Permalink
修复bitmap下载过程中加锁范围不正确导致下载不能同时进行的问题.
Browse files Browse the repository at this point in the history
  • Loading branch information
wyouflf committed May 29, 2014
1 parent 0a5c41e commit c8c5857
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
2 changes: 1 addition & 1 deletion library/src/com/lidroid/xutils/BitmapUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public <T extends View> void display(T container, String uri, BitmapDisplayConfi

final BitmapLoadTask<T> loadTask = new BitmapLoadTask<T>(container, uri, displayConfig, callBack);

// load bitmap from uri or diskCache
// get executor
PriorityExecutor executor = globalConfig.getBitmapLoadExecutor();
File diskCacheFile = this.getBitmapFileFromDiskCache(uri);
boolean diskCacheExist = diskCacheFile != null && diskCacheFile.exists();
Expand Down
57 changes: 28 additions & 29 deletions library/src/com/lidroid/xutils/bitmap/core/BitmapCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig;
import com.lidroid.xutils.util.IOUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.util.OtherUtils;
import com.lidroid.xutils.cache.FileNameGenerator;
import com.lidroid.xutils.cache.LruDiskCache;
import com.lidroid.xutils.cache.LruMemoryCache;
import com.lidroid.xutils.util.IOUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.util.OtherUtils;

import java.io.*;

Expand Down Expand Up @@ -137,7 +137,6 @@ public Bitmap downloadBitmap(String uri, BitmapDisplayConfig config, final Bitma
LruDiskCache.Snapshot snapshot = null;

try {

Bitmap bitmap = null;
// try download to disk
if (globalConfig.isDiskCacheEnabled()) {
Expand All @@ -149,36 +148,36 @@ public Bitmap downloadBitmap(String uri, BitmapDisplayConfig config, final Bitma
} catch (Throwable e) {
}
}
}

if (mDiskLruCache != null) {
try {
snapshot = mDiskLruCache.get(uri);
if (snapshot == null) {
LruDiskCache.Editor editor = mDiskLruCache.edit(uri);
if (editor != null) {
outputStream = editor.newOutputStream(DISK_CACHE_INDEX);
bitmapMeta.expiryTimestamp = globalConfig.getDownloader().downloadToStream(uri, outputStream, task);
if (bitmapMeta.expiryTimestamp < 0) {
editor.abort();
return null;
} else {
editor.setEntryExpiryTimestamp(bitmapMeta.expiryTimestamp);
editor.commit();
}
snapshot = mDiskLruCache.get(uri);
if (mDiskLruCache != null) {
try {
snapshot = mDiskLruCache.get(uri);
if (snapshot == null) {
LruDiskCache.Editor editor = mDiskLruCache.edit(uri);
if (editor != null) {
outputStream = editor.newOutputStream(DISK_CACHE_INDEX);
bitmapMeta.expiryTimestamp = globalConfig.getDownloader().downloadToStream(uri, outputStream, task);
if (bitmapMeta.expiryTimestamp < 0) {
editor.abort();
return null;
} else {
editor.setEntryExpiryTimestamp(bitmapMeta.expiryTimestamp);
editor.commit();
}
snapshot = mDiskLruCache.get(uri);
}
if (snapshot != null) {
bitmapMeta.inputStream = snapshot.getInputStream(DISK_CACHE_INDEX);
bitmap = decodeBitmapMeta(bitmapMeta, config);
if (bitmap == null) {
bitmapMeta.inputStream = null;
mDiskLruCache.remove(uri);
}
}
if (snapshot != null) {
bitmapMeta.inputStream = snapshot.getInputStream(DISK_CACHE_INDEX);
bitmap = decodeBitmapMeta(bitmapMeta, config);
if (bitmap == null) {
bitmapMeta.inputStream = null;
mDiskLruCache.remove(uri);
}
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
}
} catch (Throwable e) {
LogUtils.e(e.getMessage(), e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.util.OtherUtils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;

Expand Down Expand Up @@ -76,13 +73,14 @@ public long downloadToStream(String uri, OutputStream outputStream, final Bitmap

byte[] buffer = new byte[4096];
int len = 0;
BufferedOutputStream out = new BufferedOutputStream(outputStream);
while ((len = bis.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
out.write(buffer, 0, len);
currCount += len;
if (task.isCancelled() || task.getTargetContainer() == null) return -1;
task.updateProgress(fileLen, currCount);
}
outputStream.flush();
out.flush();
} catch (Throwable e) {
result = -1;
LogUtils.e(e.getMessage(), e);
Expand Down

0 comments on commit c8c5857

Please sign in to comment.