Permalink
Browse files

improved file not found management on images

  • Loading branch information...
1 parent e2ff249 commit 3b4371dbf9e93b04b92a1637b969f27a08650b2f @luigi-agosti luigi-agosti committed Oct 15, 2011
@@ -110,9 +110,9 @@ protected ImageCache createCache() {
}
private ImageCache initCache(Context context, Settings settings) {
- Bitmap b = bitmapUtil.decodeImageResourceAndScaleBitmap(context, settings);
ImageCache cache = createCache();
- cache.setDefaultImage(b);
+ cache.setDefaultImage(bitmapUtil.decodeDefaultAndScaleBitmap(context, settings));
+ cache.setNotFoundImage(bitmapUtil.decodeNotFoundAndScaleBitmap(context, settings));
return cache;
}
@@ -1,5 +1,7 @@
package com.novoda.imageloader.core;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Stack;
import android.app.Activity;
@@ -9,6 +11,7 @@
import android.widget.ImageView;
import com.novoda.imageloader.core.cache.ImageCache;
+import com.novoda.imageloader.core.exception.ImageNotFoundException;
public class CacheManager {
@@ -17,6 +20,7 @@
private ImageCache cache;
private PhotosLoader thread;
private Stack<Image> stack;
+ private List<String> notFoundImages;
private ImageManager imageLoader;
public static class Image {
@@ -33,6 +37,7 @@ public CacheManager(ImageManager imageLoader, ImageCache cache) {
this.imageLoader = imageLoader;
this.cache = cache;
this.stack = new Stack<Image>();
+ this.notFoundImages = new ArrayList<String>();
thread = new PhotosLoader();
thread.setPriority(Thread.NORM_PRIORITY - 1);
}
@@ -87,35 +92,48 @@ private synchronized void pushOnStack(Image p) {
@Override
public void run() {
while (true) {
- try {
- if (stack.size() == 0) {
- synchronized (this) {
- try {
- isWaiting = true;
- wait();
- } catch (Exception e) {
- Log.v(TAG, "Pausing the thread error " + e.getMessage());
- }
- }
- }
- Image image = stack.pop();
- Bitmap bmp = imageLoader.getBitmap(image.url, true);
- if (bmp == null) {
- bmp = cache.getDefaultImage();
- clean(image);
- } else {
- cache.put(image.url, bmp);
- }
- if (image.imageView.getTag() != null
- && ((String) image.imageView.getTag()).equals(image.url)) {
- BitmapDisplayer bd = new BitmapDisplayer(bmp, image.imageView);
- Activity a = (Activity) image.imageView.getContext();
- a.runOnUiThread(bd);
+ pauseThreadIfnecessary();
+ Image image = stack.pop();
+ loadAndShowImage(image);
+ }
+ }
+
+ private void pauseThreadIfnecessary() {
+ if (stack.size() == 0) {
+ synchronized (this) {
+ try {
+ isWaiting = true;
+ wait();
+ } catch (Exception e) {
+ Log.v(TAG, "Pausing the thread error " + e.getMessage());
}
- } catch (Throwable e) {
- Log.e(TAG, "Throwable : " + e.getMessage(), e);
}
+ }
+ }
+
+ private void loadAndShowImage(Image image) {
+ try {
+ Bitmap bmp = imageLoader.getBitmap(image.url, true);
+ if (bmp == null) {
+ bmp = cache.getDefaultImage();
+ clean(image);
+ } else {
+ cache.put(image.url, bmp);
+ }
+ displayImage(image, bmp);
+ } catch(ImageNotFoundException inf) {
+ notFoundImages.add(image.url);
+ } catch (Throwable e) {
+ Log.e(TAG, "Throwable : " + e.getMessage(), e);
+ }
+ }
+ private void displayImage(Image image, Bitmap bmp) {
+ if (image.imageView.getTag() != null
+ && ((String) image.imageView.getTag()).equals(image.url)) {
+ BitmapDisplayer bd = new BitmapDisplayer(bmp, image.imageView);
+ Activity a = (Activity) image.imageView.getContext();
+ a.runOnUiThread(bd);
}
}
}
@@ -140,10 +158,16 @@ public void run() {
}
public boolean hasImageInCache(String url) {
+ if(notFoundImages.contains(url)) {
+ return true;
+ }
return cache.hasImage(url);
}
public Bitmap getImageFromCache(String url) {
+ if(notFoundImages.contains(url)) {
+ return cache.getNotFoundImage();
+ }
return cache.get(url);
}
@@ -13,11 +13,14 @@
private int imageHeight;
private int imageWidth;
private int defaultImageId;
+ private int notFoundImageId;
private boolean isQueryIncludedInHash;
public Settings() {
this.expirationPeriod = DEFAULT_EXPIRATION_PERIOD;
this.setQueryIncludedInHash(DEFAULT_INCLUDE_QUERY_IN_HASH);
+ this.defaultImageId = -1;
+ this.notFoundImageId = -1;
}
public File getCacheDir() {
@@ -68,4 +71,12 @@ public void setQueryIncludedInHash(boolean isQueryIncludedInHash) {
this.isQueryIncludedInHash = isQueryIncludedInHash;
}
+ public int getNotFoundImageId() {
+ return notFoundImageId;
+ }
+
+ public void setNotFoundImageId(int notFoundImageId) {
+ this.notFoundImageId = notFoundImageId;
+ }
+
}
@@ -4,6 +4,7 @@
import android.view.Display;
import android.view.WindowManager;
+import com.novoda.imageloader.core.exception.MissingSettingException;
import com.novoda.imageloader.core.file.FileUtil;
public class SettingsBuilder {
@@ -27,6 +28,11 @@ public SettingsBuilder defaultImageId(int defaultImageId) {
return this;
}
+ public SettingsBuilder fileNotFoundImageId(int fileNotFoundImageId) {
+ settings.setNotFoundImageId(fileNotFoundImageId);
+ return this;
+ }
+
public SettingsBuilder enableQueryInHashGeneration(boolean enableQueryInHashGeneration) {
settings.setQueryIncludedInHash(enableQueryInHashGeneration);
return this;
@@ -37,6 +43,12 @@ public Settings build(Context context){
if(!isSizeSet) {
setDisplayImageSize(context);
}
+ if(settings.getDefaultImageId() == -1) {
+ throw new MissingSettingException("You need to set a default image id");
+ }
+ if(settings.getNotFoundImageId() == -1) {
+ settings.setNotFoundImageId(settings.getDefaultImageId());
+ }
return settings;
}
@@ -15,5 +15,9 @@
Bitmap getDefaultImage();
void setDefaultImage(Bitmap defaultImage);
+
+ Bitmap getNotFoundImage();
+
+ void setNotFoundImage(Bitmap defaultImage);
}
@@ -8,6 +8,7 @@
private HashMap<String, Bitmap> cache = new HashMap<String, Bitmap>();
private Bitmap defaultImage;
+ private Bitmap notFoundImage;
@Override
public boolean hasImage(String url) {
@@ -38,5 +39,13 @@ public Bitmap getDefaultImage() {
public void setDefaultImage(Bitmap defaultImage) {
this.defaultImage = defaultImage;
}
+
+ public Bitmap getNotFoundImage() {
+ return notFoundImage;
+ }
+
+ public void setNotFoundImage(Bitmap notFoundImage) {
+ this.notFoundImage = notFoundImage;
+ }
}
@@ -32,4 +32,14 @@ public void setDefaultImage(Bitmap defaultImage) {
}
+ @Override
+ public Bitmap getNotFoundImage() {
+ return null;
+ }
+
+ @Override
+ public void setNotFoundImage(Bitmap defaultImage) {
+
+ }
+
}
@@ -9,6 +9,7 @@
private HashMap<String, SoftReference<Bitmap>> cache = new HashMap<String, SoftReference<Bitmap>>();
private Bitmap defaultImage;
+ private Bitmap notFoundImage;
@Override
public boolean hasImage(String url) {
@@ -50,4 +51,14 @@ public void setDefaultImage(Bitmap defaultImage) {
this.defaultImage = defaultImage;
}
+ @Override
+ public Bitmap getNotFoundImage() {
+ return notFoundImage;
+ }
+
+ @Override
+ public void setNotFoundImage(Bitmap notFoundImage) {
+ this.notFoundImage = notFoundImage;
+ }
+
}
@@ -0,0 +1,7 @@
+package com.novoda.imageloader.core.exception;
+
+public class ImageNotFoundException extends RuntimeException {
+
+ private static final long serialVersionUID = 1L;
+
+}
@@ -0,0 +1,11 @@
+package com.novoda.imageloader.core.exception;
+
+public class MissingSettingException extends RuntimeException {
+
+ private static final long serialVersionUID = 1L;
+
+ public MissingSettingException(String message) {
+ super(message);
+ }
+
+}
@@ -10,6 +10,8 @@
import java.io.OutputStream;
import java.net.URL;
+import com.novoda.imageloader.core.exception.ImageNotFoundException;
+
import android.content.Context;
import android.util.Log;
@@ -28,7 +30,7 @@ public void retrieveImage(String url, File f) {
copyStream(is, os);
os.close();
} catch (FileNotFoundException fnfe) {
- Log.e(TAG, "Unknown Exception while getting the image" + fnfe.getMessage(), fnfe);
+ throw new ImageNotFoundException();
} catch (Exception ex) {
Log.e(TAG, "Unknown Exception while getting the image " + ex.getMessage(), ex);
} finally {
@@ -45,9 +45,16 @@ public Bitmap scaleBitmap(Bitmap b, int width, int height) {
return scaledBitmap;
}
- public Bitmap decodeImageResourceAndScaleBitmap(Context context, Settings settings) {
- Bitmap image = BitmapFactory.decodeResource(context.getResources(),
- settings.getDefaultImageId());
+ public Bitmap decodeDefaultAndScaleBitmap(Context context, Settings settings) {
+ return decodeResourceAndScale(context, settings, settings.getDefaultImageId());
+ }
+
+ public Bitmap decodeNotFoundAndScaleBitmap(Context context, Settings settings) {
+ return decodeResourceAndScale(context, settings, settings.getNotFoundImageId());
+ }
+
+ private Bitmap decodeResourceAndScale(Context context, Settings settings, int resourceId) {
+ Bitmap image = BitmapFactory.decodeResource(context.getResources(), resourceId);
return scaleBitmap(image, settings.getImageWidth(), settings.getImageHeight());
}

0 comments on commit 3b4371d

Please sign in to comment.