package net.javalib.flickr.search.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
/**
* Utility class used to deal with SD card cache.
*/
public class CacheUtil {
/** Cache root */
private static File root;
private CacheUtil() {}
static {
root = Environment.getExternalStorageDirectory();
root = new File(root,"FlickrSearch/cache");
}
public static File file(String cacheKey) {
return new File(root,cacheKey);
}
public static void deleteCache() {
deleteDir(root);
}
public static void deleteDir(File dir) {
if (!dir.isDirectory())
return;
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory())
deleteDir(file);
else {
file.delete();
}
}
}
public static boolean exists(String cacheKey) {
return file(cacheKey).exists();
}
public static Bitmap getBitmap(String cacheKey) {
if (!exists(cacheKey))
return null;
File file = file(cacheKey);
InputStream in = null;
try {
in = new FileInputStream(file);
return BitmapFactory.decodeStream(in);
} catch (Exception e) {
return null;
} finally {
if (in != null) try { in.close(); } catch (Exception e) {}
}
}
}