Skip to content

Commit bbf3b71

Browse files
committed
Issue nostra13#170 - NPE after unmount SD card
Cache images on device's file system if SD card becomes unmounted
1 parent e4c8672 commit bbf3b71

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444
*/
4545
public class DefaultConfigurationFactory {
4646

47-
/** Create {@linkplain HashCodeFileNameGenerator default implementation} of FileNameGenerator */
47+
/** Creates {@linkplain HashCodeFileNameGenerator default implementation} of FileNameGenerator */
4848
public static FileNameGenerator createFileNameGenerator() {
4949
return new HashCodeFileNameGenerator();
5050
}
5151

52-
/** Create default implementation of {@link DisckCacheAware} depends on incoming parameters */
52+
/** Creates default implementation of {@link DisckCacheAware} depends on incoming parameters */
5353
public static DiscCacheAware createDiscCache(Context context, FileNameGenerator discCacheFileNameGenerator, int discCacheSize, int discCacheFileCount) {
5454
if (discCacheSize > 0) {
5555
File individualCacheDir = StorageUtils.getIndividualCacheDirectory(context);
@@ -63,7 +63,17 @@ public static DiscCacheAware createDiscCache(Context context, FileNameGenerator
6363
}
6464
}
6565

66-
/** Create default implementation of {@link MemoryCacheAware} depends on incoming parameters */
66+
/** Creates reserve disc cache which will be used if primary disc cache becomes unavailable */
67+
public static DiscCacheAware createReserveDiscCache(Context context) {
68+
File cacheDir = context.getCacheDir();
69+
File individualDir = new File(cacheDir, "uil-images");
70+
if (individualDir.exists() || individualDir.mkdir()) {
71+
cacheDir = individualDir;
72+
}
73+
return new TotalSizeLimitedDiscCache(cacheDir, 2 * 1024 * 1024); // limit - 2 Mb
74+
}
75+
76+
/** Creates default implementation of {@link MemoryCacheAware} depends on incoming parameters */
6777
public static MemoryCacheAware<String, Bitmap> createMemoryCache(int memoryCacheSize, boolean denyCacheImageMultipleSizesInMemory) {
6878
MemoryCacheAware<String, Bitmap> memoryCache = new UsingFreqLimitedMemoryCache(memoryCacheSize);
6979
if (denyCacheImageMultipleSizesInMemory) {
@@ -72,12 +82,12 @@ public static MemoryCacheAware<String, Bitmap> createMemoryCache(int memoryCache
7282
return memoryCache;
7383
}
7484

75-
/** Create default implementation of {@link ImageDownloader} - {@link BaseImageDownloader} */
85+
/** Creates default implementation of {@link ImageDownloader} - {@link BaseImageDownloader} */
7686
public static ImageDownloader createImageDownloader(Context context) {
7787
return new BaseImageDownloader(context);
7888
}
7989

80-
/** Create default implementation of {@link BitmapDisplayer} */
90+
/** Creates default implementation of {@link BitmapDisplayer} */
8191
public static BitmapDisplayer createBitmapDisplayer() {
8292
return new SimpleBitmapDisplayer();
8393
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public final class ImageLoaderConfiguration {
6767
final ThreadFactory displayImageThreadFactory;
6868
final boolean loggingEnabled;
6969

70+
final DiscCacheAware reserveDiscCache;
7071
final ImageDownloader networkDeniedDownloader;
7172
final ImageDownloader slowNetworkDownloader;
7273

@@ -97,6 +98,8 @@ public Thread newThread(Runnable r) {
9798

9899
networkDeniedDownloader = new NetworkDeniedImageDownloader(downloader);
99100
slowNetworkDownloader = new SlowNetworkImageDownloader(downloader);
101+
102+
reserveDiscCache = DefaultConfigurationFactory.createReserveDiscCache(context);
100103
}
101104

102105
/**

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ private boolean checkTaskIsInterrupted() {
216216
private Bitmap tryLoadBitmap() {
217217
DiscCacheAware discCache = configuration.discCache;
218218
File imageFile = discCache.get(uri);
219+
File cacheDir = imageFile.getParentFile();
220+
if (cacheDir == null || (!cacheDir.exists() && !cacheDir.mkdirs())) {
221+
imageFile = configuration.reserveDiscCache.get(uri);
222+
}
219223

220224
Bitmap bitmap = null;
221225
try {
@@ -314,11 +318,6 @@ private Bitmap decodeWithOOMHandling(URI imageUri) throws IOException {
314318
}
315319

316320
private void saveImageOnDisc(File targetFile) throws IOException, URISyntaxException {
317-
File cacheDir = targetFile.getParentFile();
318-
if (cacheDir == null || !cacheDir.exists()) {
319-
cacheDir.mkdirs();
320-
}
321-
322321
int width = configuration.maxImageWidthForDiscCache;
323322
int height = configuration.maxImageHeightForDiscCache;
324323
if (width > 0 || height > 0) {

0 commit comments

Comments
 (0)