Skip to content

Commit 85808ab

Browse files
committed
Issue nostra13#309 - Introduced bitmap processor for disc cache (in ImageLoaderConfiguration.discCacheExtraOptions(...))
1 parent a299f48 commit 85808ab

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

library/src/com/nostra13/universalimageloader/core/ImageLoaderConfiguration.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.nostra13.universalimageloader.core.download.ImageDownloader;
3333
import com.nostra13.universalimageloader.core.download.NetworkDeniedImageDownloader;
3434
import com.nostra13.universalimageloader.core.download.SlowNetworkImageDownloader;
35+
import com.nostra13.universalimageloader.core.process.BitmapProcessor;
3536
import com.nostra13.universalimageloader.utils.L;
3637

3738
/**
@@ -56,6 +57,7 @@ public final class ImageLoaderConfiguration {
5657
final int maxImageHeightForDiscCache;
5758
final CompressFormat imageCompressFormatForDiscCache;
5859
final int imageQualityForDiscCache;
60+
final BitmapProcessor processorForDiscCache;
5961

6062
final Executor taskExecutor;
6163
final Executor taskExecutorForCachedImages;
@@ -85,6 +87,7 @@ private ImageLoaderConfiguration(final Builder builder) {
8587
maxImageHeightForDiscCache = builder.maxImageHeightForDiscCache;
8688
imageCompressFormatForDiscCache = builder.imageCompressFormatForDiscCache;
8789
imageQualityForDiscCache = builder.imageQualityForDiscCache;
90+
processorForDiscCache = builder.processorForDiscCache;
8891
taskExecutor = builder.taskExecutor;
8992
taskExecutorForCachedImages = builder.taskExecutorForCachedImages;
9093
threadPoolSize = builder.threadPoolSize;
@@ -159,6 +162,7 @@ public static class Builder {
159162
private int maxImageHeightForDiscCache = 0;
160163
private CompressFormat imageCompressFormatForDiscCache = null;
161164
private int imageQualityForDiscCache = 0;
165+
private BitmapProcessor processorForDiscCache = null;
162166

163167
private Executor taskExecutor = null;
164168
private Executor taskExecutorForCachedImages = null;
@@ -211,12 +215,14 @@ public Builder memoryCacheExtraOptions(int maxImageWidthForMemoryCache, int maxI
211215
* save them at disc cache
212216
* @param compressQuality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress
213217
* for max quality. Some formats, like PNG which is lossless, will ignore the quality setting
218+
* @param processorForDiscCache null-ok; {@linkplain BitmapProcessor Bitmap processor} which process images before saving them in disc cache
214219
*/
215-
public Builder discCacheExtraOptions(int maxImageWidthForDiscCache, int maxImageHeightForDiscCache, CompressFormat compressFormat, int compressQuality) {
220+
public Builder discCacheExtraOptions(int maxImageWidthForDiscCache, int maxImageHeightForDiscCache, CompressFormat compressFormat, int compressQuality, BitmapProcessor processorForDiscCache) {
216221
this.maxImageWidthForDiscCache = maxImageWidthForDiscCache;
217222
this.maxImageHeightForDiscCache = maxImageHeightForDiscCache;
218223
this.imageCompressFormatForDiscCache = compressFormat;
219224
this.imageQualityForDiscCache = compressQuality;
225+
this.processorForDiscCache = processorForDiscCache;
220226
return this;
221227
}
222228

library/src/com/nostra13/universalimageloader/core/LoadAndDisplayImageTask.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ final class LoadAndDisplayImageTask implements Runnable {
6666
private static final String LOG_POSTPROCESS_IMAGE = "PostProcess image before displaying [%s]";
6767
private static final String LOG_CACHE_IMAGE_IN_MEMORY = "Cache image in memory [%s]";
6868
private static final String LOG_CACHE_IMAGE_ON_DISC = "Cache image on disc [%s]";
69+
private static final String LOG_PROCESS_IMAGE_BEFORE_CACHE_ON_DISC = "Process image before cache on disc [%s]";
6970
private static final String LOG_TASK_CANCELLED = "ImageView is reused for another image. Task is cancelled. [%s]";
7071
private static final String LOG_TASK_INTERRUPTED = "Task was interrupted [%s]";
7172

72-
private static final String WARNING_PRE_PROCESSOR_NULL = "Pre-processor returned null [%s]";
73-
private static final String WARNING_POST_PROCESSOR_NULL = "Pre-processor returned null [%s]";
73+
private static final String ERROR_PRE_PROCESSOR_NULL = "Pre-processor returned null [%s]";
74+
private static final String ERROR_POST_PROCESSOR_NULL = "Pre-processor returned null [%s]";
75+
private static final String ERROR_PROCESSOR_FOR_DISC_CACHE_NULL = "Bitmap processor for disc cache returned null [%s]";
7476

7577
private static final int BUFFER_SIZE = 8 * 1024; // 8 Kb
7678

@@ -132,15 +134,15 @@ public void run() {
132134
bmp = configuration.memoryCache.get(memoryCacheKey);
133135
if (bmp == null) {
134136
bmp = tryLoadBitmap();
135-
if (bmp == null) return;
137+
if (bmp == null) return; // listener callback already was fired
136138

137139
if (checkTaskIsNotActual() || checkTaskIsInterrupted()) return;
138140

139141
if (options.shouldPreProcess()) {
140142
log(LOG_PREPROCESS_IMAGE);
141143
bmp = options.getPreProcessor().process(bmp);
142144
if (bmp == null) {
143-
L.w(WARNING_PRE_PROCESSOR_NULL);
145+
L.e(ERROR_PRE_PROCESSOR_NULL);
144146
}
145147
}
146148

@@ -157,7 +159,7 @@ public void run() {
157159
log(LOG_POSTPROCESS_IMAGE);
158160
bmp = options.getPostProcessor().process(bmp);
159161
if (bmp == null) {
160-
L.w(WARNING_POST_PROCESSOR_NULL, memoryCacheKey);
162+
L.e(ERROR_POST_PROCESSOR_NULL, memoryCacheKey);
161163
}
162164
}
163165
} finally {
@@ -210,7 +212,7 @@ private boolean delayIfNeed() {
210212

211213
/**
212214
* Check whether the image URI of this task matches to image URI which is actual for current ImageView at this
213-
* moment and fire {@link ImageLoadingListener#onLoadingCancelled()} event if it doesn't.
215+
* moment and fire {@link ImageLoadingListener#onLoadingCancelled(String, android.view.View)}} event if it doesn't.
214216
*/
215217
private boolean checkTaskIsNotActual() {
216218
String currentCacheKey = engine.getLoadingUriForView(imageView);
@@ -328,18 +330,25 @@ private boolean downloadSizedImage(File targetFile, int maxWidth, int maxHeight)
328330
DisplayImageOptions specialOptions = new DisplayImageOptions.Builder().cloneFrom(options).imageScaleType(ImageScaleType.IN_SAMPLE_INT).build();
329331
ImageDecodingInfo decodingInfo = new ImageDecodingInfo(memoryCacheKey, uri, targetImageSize, ViewScaleType.FIT_INSIDE, getDownloader(), specialOptions);
330332
Bitmap bmp = decoder.decode(decodingInfo);
331-
boolean savedSuccessfully = false;
332-
if (bmp != null) {
333-
OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile), BUFFER_SIZE);
334-
try {
335-
savedSuccessfully = bmp.compress(configuration.imageCompressFormatForDiscCache, configuration.imageQualityForDiscCache, os);
336-
} finally {
337-
IoUtils.closeSilently(os);
338-
}
339-
if (savedSuccessfully) {
340-
bmp.recycle();
333+
if (bmp == null) return false;
334+
335+
if (configuration.processorForDiscCache != null) {
336+
log(LOG_PROCESS_IMAGE_BEFORE_CACHE_ON_DISC);
337+
bmp = configuration.processorForDiscCache.process(bmp);
338+
if (bmp == null) {
339+
L.e(ERROR_PROCESSOR_FOR_DISC_CACHE_NULL, memoryCacheKey);
340+
return false;
341341
}
342342
}
343+
344+
OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile), BUFFER_SIZE);
345+
boolean savedSuccessfully;
346+
try {
347+
savedSuccessfully = bmp.compress(configuration.imageCompressFormatForDiscCache, configuration.imageQualityForDiscCache, os);
348+
} finally {
349+
IoUtils.closeSilently(os);
350+
}
351+
bmp.recycle();
343352
return savedSuccessfully;
344353
}
345354

0 commit comments

Comments
 (0)