Skip to content

Commit

Permalink
修复偶发的磁盘缓存开启的情况下图片无法加载的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wyouflf committed Jul 4, 2014
1 parent 2ad2bc4 commit 07c330b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
16 changes: 8 additions & 8 deletions library/src/com/lidroid/xutils/bitmap/BitmapDisplayConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import android.graphics.drawable.Drawable;
import android.view.animation.Animation;
import com.lidroid.xutils.bitmap.core.BitmapSize;
import com.lidroid.xutils.bitmap.factory.ImageFactory;
import com.lidroid.xutils.bitmap.factory.BitmapFactory;
import com.lidroid.xutils.task.Priority;

public class BitmapDisplayConfig {
Expand All @@ -31,7 +31,7 @@ public class BitmapDisplayConfig {
private boolean autoRotation = false;
private boolean showOriginal = false;
private Bitmap.Config bitmapConfig = Bitmap.Config.RGB_565;
private ImageFactory imageFactory;
private BitmapFactory bitmapFactory;

private Priority priority;

Expand Down Expand Up @@ -94,12 +94,12 @@ public void setBitmapConfig(Bitmap.Config bitmapConfig) {
this.bitmapConfig = bitmapConfig;
}

public ImageFactory getImageFactory() {
return imageFactory;
public BitmapFactory getBitmapFactory() {
return bitmapFactory;
}

public void setImageFactory(ImageFactory imageFactory) {
this.imageFactory = imageFactory;
public void setBitmapFactory(BitmapFactory bitmapFactory) {
this.bitmapFactory = bitmapFactory;
}

public Priority getPriority() {
Expand All @@ -113,7 +113,7 @@ public void setPriority(Priority priority) {
@Override
public String toString() {
return (isShowOriginal() ? "" : bitmapMaxSize.toString()) +
(imageFactory == null ? "" : imageFactory.getClass().getName());
(bitmapFactory == null ? "" : bitmapFactory.getClass().getName());
}

public BitmapDisplayConfig cloneNew() {
Expand All @@ -125,7 +125,7 @@ public BitmapDisplayConfig cloneNew() {
config.autoRotation = this.autoRotation;
config.showOriginal = this.showOriginal;
config.bitmapConfig = this.bitmapConfig;
config.imageFactory = this.imageFactory;
config.bitmapFactory = this.bitmapFactory;
config.priority = this.priority;
return config;
}
Expand Down
5 changes: 5 additions & 0 deletions library/src/com/lidroid/xutils/bitmap/BitmapGlobalConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.lidroid.xutils.bitmap.core.BitmapCache;
import com.lidroid.xutils.bitmap.download.DefaultDownloader;
import com.lidroid.xutils.bitmap.download.Downloader;
import com.lidroid.xutils.task.Priority;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.util.OtherUtils;
import com.lidroid.xutils.task.PriorityAsyncTask;
Expand Down Expand Up @@ -238,6 +239,10 @@ private class BitmapCacheManagementTask extends PriorityAsyncTask<Object, Void,
public static final int MESSAGE_CLEAR_MEMORY_BY_KEY = 8;
public static final int MESSAGE_CLEAR_DISK_BY_KEY = 9;

private BitmapCacheManagementTask() {
this.setPriority(Priority.UI_TOP);
}

@Override
protected Object[] doInBackground(Object... params) {
if (params == null || params.length == 0) return params;
Expand Down
19 changes: 12 additions & 7 deletions library/src/com/lidroid/xutils/bitmap/core/BitmapCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig;
import com.lidroid.xutils.bitmap.factory.BitmapFactory;
import com.lidroid.xutils.cache.FileNameGenerator;
import com.lidroid.xutils.cache.LruDiskCache;
import com.lidroid.xutils.cache.LruMemoryCache;
Expand Down Expand Up @@ -106,8 +107,8 @@ public void initDiskCache() {
}
}
}
mDiskCacheLock.notifyAll();
isDiskCacheReady = true;
mDiskCacheLock.notifyAll();
}
}

Expand Down Expand Up @@ -196,10 +197,7 @@ public Bitmap downloadBitmap(String uri, BitmapDisplayConfig config, final Bitma

if (bitmap != null) {
bitmap = rotateBitmapIfNeeded(uri, config, bitmap);
if (config != null && config.getImageFactory() != null) {
bitmap = config.getImageFactory().createBitmap(bitmap);
}
addBitmapToMemoryCache(uri, config, bitmap, bitmapMeta.expiryTimestamp);
bitmap = addBitmapToMemoryCache(uri, config, bitmap, bitmapMeta.expiryTimestamp);
}
return bitmap;
} catch (Throwable e) {
Expand All @@ -212,11 +210,18 @@ public Bitmap downloadBitmap(String uri, BitmapDisplayConfig config, final Bitma
return null;
}

private void addBitmapToMemoryCache(String uri, BitmapDisplayConfig config, Bitmap bitmap, long expiryTimestamp) throws IOException {
private Bitmap addBitmapToMemoryCache(String uri, BitmapDisplayConfig config, Bitmap bitmap, long expiryTimestamp) throws IOException {
if (config != null) {
BitmapFactory bitmapFactory = config.getBitmapFactory();
if (bitmapFactory != null) {
bitmap = bitmapFactory.cloneNew().createBitmap(bitmap);
}
}
if (uri != null && bitmap != null && globalConfig.isMemoryCacheEnabled() && mMemoryCache != null) {
MemoryCacheKey key = new MemoryCacheKey(uri, config);
mMemoryCache.put(key, bitmap, expiryTimestamp);
}
return bitmap;
}

/**
Expand Down Expand Up @@ -281,7 +286,7 @@ public Bitmap getBitmapFromDiskCache(String uri, BitmapDisplayConfig config) {
}

bitmap = rotateBitmapIfNeeded(uri, config, bitmap);
addBitmapToMemoryCache(uri, config, bitmap, mDiskLruCache.getExpiryTimestamp(uri));
bitmap = addBitmapToMemoryCache(uri, config, bitmap, mDiskLruCache.getExpiryTimestamp(uri));
return bitmap;
}
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* Date: 14-05-20
* Time: 下午4:26
*/
public interface ImageFactory {
public interface BitmapFactory {

BitmapFactory cloneNew();

Bitmap createBitmap(Bitmap rawBitmap);
}

0 comments on commit 07c330b

Please sign in to comment.