Permalink
Browse files

some change due to bug fix of the imageloader

  • Loading branch information...
1 parent 1291f5e commit 8fd6d2af69d94f13bf29cc2fff1e20d8da997c0b @luigi-agosti luigi-agosti committed Aug 10, 2011
View
8 imageLoader/src/main/java/com/novoda/lib/imageloader/BaseImageLoader.java
@@ -22,12 +22,12 @@
private CacheManager cacheManager;
private Settings settings;
- public BaseImageLoader(Settings settings) {
+ public BaseImageLoader(Context context, Settings settings) {
this.bitmapUtil = new BitmapUtil();
this.settings = settings;
this.cacheManager = new CacheManager(this, createCache(),
- bitmapUtil.decodeImageResourceAndScaleBitmap(settings));
- sendCacheCleanUpBroadcast(settings.getContext(), settings.getExpirationPeriod());
+ bitmapUtil.decodeImageResourceAndScaleBitmap(context, settings));
+ sendCacheCleanUpBroadcast(context, settings.getExpirationPeriod());
}
@Override
@@ -53,7 +53,7 @@ public Bitmap getBitmap(String url) {
@Override
public Bitmap getBitmap(String url, boolean scale) {
- if (url != null && url.length() != 0) {
+ if (url != null && url.length() >= 0) {
String filename = String.valueOf(url.hashCode());
File f = new File(settings.getCacheDir(), filename);
if (f.exists()) {
View
10 imageLoader/src/main/java/com/novoda/lib/imageloader/CacheManager.java
@@ -4,6 +4,7 @@
import android.app.Activity;
import android.graphics.Bitmap;
+import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
@@ -39,8 +40,11 @@ public CacheManager(ImageManager imageLoader, ImageCache cache, Bitmap defaultIm
}
public void push(Image p) {
- pushOnStack(p);
p.imageView.setImageBitmap(defaultImage);
+ if(TextUtils.isEmpty(p.url)) {
+ return;
+ }
+ pushOnStack(p);
if(thread.getState() == Thread.State.NEW) {
thread.start();
} else {
@@ -68,7 +72,7 @@ public synchronized Image pop() {
private synchronized void clean(Image p) {
for (int j = 0; j < stack.size(); j++) {
- if (stack.get(j).url.equals(p.url)) {
+ if (stack.get(j).url != null && stack.get(j).url.equals(p.url)) {
stack.remove(j);
j--;
}
@@ -103,7 +107,7 @@ public void run() {
} else {
cache.put(image.url, bmp);
}
- if(((String)image.imageView.getTag()).equals(image.url)){
+ 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);
View
26 imageLoader/src/main/java/com/novoda/lib/imageloader/Settings.java
@@ -10,24 +10,28 @@
public class Settings {
//It is better to consider period greater than 1500 millisec
- public static final long EXPIRATION_PERIOD = 7l*24l*3600l*1000l;
+ private static final long EXPIRATION_PERIOD_DEFAULT = 7l*24l*3600l*1000l;
private static final String DEFAULT_NAME = "imagedata";
- private Context context;
private File cacheDir;
private int imageHeight;
private int imageWidth;
private int defaultImageId;
+ private long expirationPeriod;
private SoftReference<Bitmap> defaultBitmap;
public Settings(Context context) {
- this(context, 500, 500, -1);
+ this(context, 500, 500, -1, EXPIRATION_PERIOD_DEFAULT);
}
public Settings(Context context, int imageHeight, int imageWidth, int defaultImageId) {
- this.context = context;
+ this(context, imageHeight, imageWidth, defaultImageId, EXPIRATION_PERIOD_DEFAULT);
+ }
+
+ public Settings(Context context, int imageHeight, int imageWidth, int defaultImageId, long fileCacheExpirationPeriod) {
this.cacheDir = prepareCacheDir(context);
this.imageHeight = imageHeight;
this.imageWidth = imageWidth;
+ this.expirationPeriod = fileCacheExpirationPeriod;
this.defaultImageId = defaultImageId;
}
@@ -44,21 +48,17 @@ public File getCacheDir() {
}
public long getExpirationPeriod() {
- return EXPIRATION_PERIOD;
+ return expirationPeriod;
}
- public Bitmap getDefaultBitmap() {
+ public Bitmap getDefaultBitmap(Context context) {
if(defaultBitmap != null) {
return defaultBitmap.get();
}
Bitmap b = BitmapFactory.decodeResource(context.getResources(), defaultImageId);
defaultBitmap = new SoftReference<Bitmap>(b);
return b;
}
-
- public Context getContext() {
- return context;
- }
private boolean isMounted() {
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
@@ -70,10 +70,6 @@ private boolean isMounted() {
private File getExternalStorageDirectory() {
return android.os.Environment.getExternalStorageDirectory();
}
-
- private File getSystemCacheDir() {
- return context.getCacheDir();
- }
private String getDefaultPath(Context context) {
String packageName = context.getApplicationContext().getPackageName();
@@ -85,7 +81,7 @@ private File prepareCacheDir(Context context) {
if (isMounted()){
cacheDir = new File(getExternalStorageDirectory(), getDefaultPath(context));
} else{
- cacheDir = getSystemCacheDir();
+ cacheDir = context.getCacheDir();
}
if(!cacheDir.exists()){
cacheDir.mkdirs();
View
5 imageLoader/src/main/java/com/novoda/lib/imageloader/file/FileUtil.java
@@ -65,10 +65,9 @@ public boolean deleteFileCache(String cacheDirFullPath) {
public boolean reduceFileCache(String cacheDirFullPath, long expirationPeriod) {
File cacheDir = new File(cacheDirFullPath);
if (cacheDir.isDirectory()) {
- String[] children = cacheDir.list();
+ File[] children = cacheDir.listFiles();
long lastModifiedThreashold = System.currentTimeMillis() - expirationPeriod;
- for (int i = 0; i < children.length; i++) {
- File f = new File(cacheDir, children[i]);
+ for (File f: children) {
if(f.lastModified() < lastModifiedThreashold) {
f.delete();
}
View
6 imageLoader/src/main/java/com/novoda/lib/imageloader/util/BitmapUtil.java
@@ -4,6 +4,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
@@ -16,6 +17,7 @@
public Bitmap decodeFileAndScale(File f, boolean scale, Settings settings){
try {
+ f.setLastModified(System.currentTimeMillis());
Bitmap unscaledBitmap = BitmapFactory.decodeStream(new FileInputStream(f));
return scaleBitmap(unscaledBitmap, settings.getImageWidth(), settings.getImageHeight());
} catch (FileNotFoundException e) {
@@ -43,8 +45,8 @@ public Bitmap scaleBitmap(Bitmap b, int width, int height){
return scaledBitmap;
}
- public Bitmap decodeImageResourceAndScaleBitmap(Settings settings){
- return scaleBitmap(settings.getDefaultBitmap(), settings.getImageWidth(), settings.getImageHeight());
+ public Bitmap decodeImageResourceAndScaleBitmap(Context context, Settings settings){
+ return scaleBitmap(settings.getDefaultBitmap(context), settings.getImageWidth(), settings.getImageHeight());
}
}
View
9 ...erTester/src/main/java/com/novoda/lib/imageloadertester/ImageLoaderTesterApplication.java
@@ -1,6 +1,9 @@
package com.novoda.lib.imageloadertester;
import android.app.Application;
+import android.content.Context;
+import android.view.Display;
+import android.view.WindowManager;
import com.novoda.lib.imageloader.BaseImageLoader;
import com.novoda.lib.imageloader.ImageManager;
@@ -15,8 +18,10 @@
public void onCreate() {
super.onCreate();
//TODO add this to your classs
- Settings settings = new Settings(this, 150, 150, R.drawable.bg_img_loading);
- imageLoader = new BaseImageLoader(settings);
+ Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE))
+ .getDefaultDisplay();
+ Settings settings = new Settings(this, display.getHeight(), display.getWidth(), R.drawable.bg_img_loading);
+ imageLoader = new BaseImageLoader(this, settings);
//
}

0 comments on commit 8fd6d2a

Please sign in to comment.