Android 缓存机制(二)
来源:互联网 发布:ubuntu paichu fuzhi 编辑:程序博客网 时间:2024/06/05 04:53
外部文件缓存
[html] view plaincopy
private File mCacheDir = context.getCacheDir();
private static final int MAX_CACHE_SIZE = 20 * 1024 * 1024; //20M
private final LruCache<String, Long> sFileCache = new LruCache<String, Long>(MAX_CACHE_SIZE){
@Override
public int sizeOf(String key, Long value){
return value.intValue();
}
@Override
protected void entryRemoved(boolean evicted, String key, Long oldValue, Long newValue){
File file = getFile(key);
if(file != null)
file.delete();
}
}
private File getFile(String fileName) throws FileNotFoundException {
File file = new File(mCacheDir, fileName);
if(!file.exists() || !file.isFile())
throw new FileNotFoundException("文件不存在或有同名文件夹");
return file;
}
//缓存bitmap到外部存储
public boolean putBitmap(String key, Bitmap bitmap){
File file = getFile(key);
if(file != null){
Log.v("tag", "文件已经存在");
return true;
}
FileOutputStream fos = getOutputStream(key);
boolean saved = bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
if(saved){
synchronized(sFileCache){
sFileCache.put(key, getFile(key).length());
}
return true;
}
return false;
}
//根据key获取OutputStream
private FileOutputStream getOutputStream(String key){
if(mCacheDir == null)
return null;
FileOutputStream fos = new FileOutputStream(mCacheDir.getAbsolutePath() + File.separator + key);
return fos;
}
//获取bitmap
private static BitmapFactory.Options sBitmapOptions;
static {
sBitmapOptions = new BitmapFactory.Options();
sBitmapOptions.inPurgeable=true; //bitmap can be purged to disk
}
public Bitmap getBitmap(String key){
File bitmapFile = getFile(key);
if(bitmapFile != null){
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(bitmapFile), null, sBitmapOptions);
if(bitmap != null){
//重新将其缓存至硬引用中
...
}
}
}
[html] view plaincopy
private File mCacheDir = context.getCacheDir();
private static final int MAX_CACHE_SIZE = 20 * 1024 * 1024; //20M
private final LruCache<String, Long> sFileCache = new LruCache<String, Long>(MAX_CACHE_SIZE){
@Override
public int sizeOf(String key, Long value){
return value.intValue();
}
@Override
protected void entryRemoved(boolean evicted, String key, Long oldValue, Long newValue){
File file = getFile(key);
if(file != null)
file.delete();
}
}
private File getFile(String fileName) throws FileNotFoundException {
File file = new File(mCacheDir, fileName);
if(!file.exists() || !file.isFile())
throw new FileNotFoundException("文件不存在或有同名文件夹");
return file;
}
//缓存bitmap到外部存储
public boolean putBitmap(String key, Bitmap bitmap){
File file = getFile(key);
if(file != null){
Log.v("tag", "文件已经存在");
return true;
}
FileOutputStream fos = getOutputStream(key);
boolean saved = bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
if(saved){
synchronized(sFileCache){
sFileCache.put(key, getFile(key).length());
}
return true;
}
return false;
}
//根据key获取OutputStream
private FileOutputStream getOutputStream(String key){
if(mCacheDir == null)
return null;
FileOutputStream fos = new FileOutputStream(mCacheDir.getAbsolutePath() + File.separator + key);
return fos;
}
//获取bitmap
private static BitmapFactory.Options sBitmapOptions;
static {
sBitmapOptions = new BitmapFactory.Options();
sBitmapOptions.inPurgeable=true; //bitmap can be purged to disk
}
public Bitmap getBitmap(String key){
File bitmapFile = getFile(key);
if(bitmapFile != null){
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(bitmapFile), null, sBitmapOptions);
if(bitmap != null){
//重新将其缓存至硬引用中
...
}
}
}
0 0
- Android 缓存机制(二)
- Android 缓存机制(二)
- hibernate缓存机制(二)
- iOS缓存机制(二)
- iOS缓存机制(二)
- hibernate缓存机制(二)
- Android 缓存机制(一)
- Android 缓存机制(三)
- Android 缓存机制(一)
- 浏览器缓存机制浅析(二)
- 缓存机制 Cache ARC算法(二)
- Mybatis的缓存机制(二)
- Hibernate二缓存机制
- iOS开发之缓存(二):本地缓存机制
- 第十一章 缓存机制(二) Realm 缓存
- Android 缓存机制代码(软引用)
- Android图片缓存机制(Caching Bitmaps)
- android缓存机制
- STM32F103/ <1>开发环境IDE的选择
- Java 9,OSGi以及模块化的未来
- 第五届蓝桥杯软件类省赛真题-C-A-3_神奇算式
- 设计模式-策略模式(3)
- Codeforces Round #381 (Div. 2) E. Alyona and towers
- Android 缓存机制(二)
- File常用的方法
- 开发:个人理财_CDUESTC
- dbms_ddl
- 【Android - 框架】之OkHttp的使用
- Dump文件介绍与使用
- |算法讨论|贪心算法 学习笔记
- xgboost特征选择
- 文字跳动特效