Skip to content

Instantly share code, notes, and snippets.

@kimukou
Forked from kojiokb/ImageCache.java
Last active October 11, 2015 21:58
Show Gist options
  • Save kimukou/3926084 to your computer and use it in GitHub Desktop.
Save kimukou/3926084 to your computer and use it in GitHub Desktop.
LruCacheの使い方
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
public final class ImageCache {
private static final String TAG = "ImageCache";
private static final int MEM_CACHE_SIZE = 1 * 1024 * 1024; // 1MB
private static LruCache<String, Bitmap> sLruCache;
static {
sLruCache = new LruCache<String, Bitmap>(MEM_CACHE_SIZE) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight();
}
};
}
private ImageCache() {
}
public static void put(String key, Bitmap bitmap) {
Bitmap bmp = get(key);
if (bmp == null || bmp.isRecycled()) {
sLruCache.put(key, bitmap);
}
}
public static Bitmap get(String key) {
Bitmap bmp = sLruCache.get(key);
return bmp==null ? null:bmp.isRecycled()? null:bmp;
}
public static Activity activity;
public static void init(Activity activity_){
activity = activity_;
}
public static int differenceDays(Date date1,Date date2) {
long datetime1 = date1.getTime();
long datetime2 = date2.getTime();
long one_date_time = 1000 * 60 * 60 * 24;
long diffDays = (datetime1 - datetime2) / one_date_time;
return (int)diffDays;
}
public static boolean loadIconList(){
return loadIconList(true);
}
private final static String path = "SaveIconList.dat";
@SuppressWarnings("unchecked")
public static boolean loadIconList(boolean init_f){
try {
File iconFile = null;
FileInputStream fis = null;
if(isSDCardWriteReady()){
File dir = getSDCardDir();
// 新規のファイルオブジェクトを作成
iconFile = new File(dir, path);
if(iconFile.exists())fis = new FileInputStream(iconFile);
}
else{
iconFile = activity.getFileStreamPath(path);
if(iconFile.exists())fis = activity.openFileInput(path);
}
if(!iconFile.exists())return false;
//一週間超えていたらキャッシュは消す
if(init_f){
Date lastModDate = new Date(iconFile.lastModified());
if(differenceDays(new Date(),lastModDate)>=7){
iconFile.delete();
return false;
}
}
long length = iconFile.length();
Log.d(TAG, "[loadIconList]length = " + length);
ObjectInputStream ois = new ObjectInputStream(fis);
Map<String,byte[]> iconList = (HashMap<String,byte[]>) ois.readObject();
ois.close();
//byte => bitmap変換
for(String key:iconList.keySet()){
byte[] bytSig = iconList.get(key);
Bitmap value = BitmapFactory.decodeByteArray(bytSig, 0, bytSig.length);
sLruCache.put(key, value);
}
Log.d(TAG, "[loadIconList]length = " + iconList.size());
} catch (Exception e) {
Log.e(TAG, "loadIconList ",e);
return false;
}
return true;
}
public static boolean saveIconList(){
if(sLruCache.size()<=0)return false;
FileOutputStream fos = null;
try {
File iconFile = null;
if(isSDCardWriteReady()){
File dir = getSDCardDir();
iconFile = new File(dir, path);
fos = new FileOutputStream(iconFile);
}
else{
iconFile = activity.getFileStreamPath(path);
fos = activity.openFileOutput(path, Context.MODE_PRIVATE);
}
Map<String,byte[]> iconList = new HashMap<String,byte[]>();
//bmp => byte変換
Map<String,Bitmap> snap = sLruCache.snapshot();
for(String key:snap.keySet()){
Bitmap bmp = snap.get(key);
int size = bmp.getWidth() * bmp.getHeight();
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
iconList.put(key, out.toByteArray());
}
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(iconList);
oos.close();
long length = iconFile.length();
Log.d(TAG, "[saveIconList]length = " + length);
return true;
} catch (Exception e) {
Log.e(TAG, "saveIconList ",e);
return false;
}
finally{
try {
if(fos!=null)fos.close();
} catch (IOException e) {}
}
}
private static boolean isSDCardWriteReady(){
if (activity.checkCallingOrSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)return false;
String state = Environment.getExternalStorageState();
return (Environment.MEDIA_MOUNTED.equals(state) && !Environment.MEDIA_MOUNTED_READ_ONLY.equals(state));
}
private static File getSDCardDir(){
File dir = null;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO){
dir = ActivityUtil.getExternalFilesDir(activity,null);
}
else{
dir = new File(Environment.getExternalStorageDirectory(),"/Android/data/" + activity.getClass().getPackage().getName() +"/files");
dir.mkdirs();
}
return dir;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment