android的图片缓存,Android 图片缓存策略

public class DiscCache {

private static final int MAX = 50 * 1024 * 1024;

private Context mContext;

private DiskLruCache mDiskLruCache;

public DiscCache(Context context) {

mContext = context;

String path = FileUtils.getBitmapCachePath(mContext);

final File cacheDir = new File(path);

if (!cacheDir.exists()) {

cacheDir.mkdirs();

}

ThreadManager.startRun(new Runnable() {

@Override

public void run() {

mDiskLruCache = DiskLruCache.open(cacheDir, 1, 1, MAX);

}

});

}

/**

* 增加获取缩略图Bitmap

*

* @param url

* @param config

* @return

*/

public Bitmap getThumbnailBitmap(String url, BitmapConfig config) {

Bitmap bitmap = getBitmapFromDisk(url, config);

try {

bitmap = LetvThumbnailUtils.getThumbnailBitmap(url);

if (bitmap == null) {

NativeThumbnail nativeThumbnail = new NativeThumbnail(url);

bitmap = nativeThumbnail.getVideoThumbnail(96, 96, 10);

}

if (bitmap != null && mDiskLruCache != null) {

DiskLruCache.Editor editor = mDiskLruCache.edit(MD5.MD5Encode(url));

if (editor != null) {

OutputStream outputStream = editor.newOutputStream(0);

bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);

editor.commit();

}

}

} catch (Exception e) {

e.printStackTrace();

}

return bitmap;

}

/**

* 从网络获取bitmap

*

* @param url

* @param config

* @return

*/

public Bitmap getBitmapFromNetwork(String url, BitmapConfig config) {

try {

DiskLruCache.Editor editor = null;

if (mDiskLruCache != null) {

editor = mDiskLruCache.edit(MD5.MD5Encode(url));

}

if (editor == null) {

return getBitmapFromNetworkWithoutLru(url, config);

}

OutputStream outputStream = editor.newOutputStream(0);

if (DownloaderFromHttp.download(url, outputStream)) {

editor.commit();

} else {

editor.abort();

}

return getBitmapFromDisk(url, config);

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

/**

* 从网络获取bitmap

*

* @param url

* @param config

* @return

*/

private Bitmap getBitmapFromNetworkWithoutLru(String url, BitmapConfig config) {

byte[] data = DownloaderFromHttp.download(url);

if (data != null && data.length > 0) {

ImgFileManager.saveImage(mContext, url, data);

if (config == null) {

return BitmapDecoder.decodeSampledBitmapFromByteArray(data, 0, data.length);

} else {

return BitmapDecoder

.decodeSampledBitmapFromByteArray(data, 0, data.length, config.width, config.height);

}

}

return null;

}

/**

* 从磁盘获取bitmap

*

* @param key

* @return

*/

public Bitmap getBitmapFromDisk(String key, BitmapConfig config) {

Snapshot snapshot = null;

try {

if (mDiskLruCache != null)

snapshot = mDiskLruCache.get(MD5.MD5Encode(key));

} catch (IOException e1) {

e1.printStackTrace();

}

if (snapshot == null) {

return getBitmapFromDiskWithoutLru(key, config);

}

FileInputStream fis = null;

ByteArrayOutputStream bos = null;

try {

fis = (FileInputStream) snapshot.getInputStream(0);

if (fis == null) {

return getBitmapFromDiskWithoutLru(key, config);

}

bos = new ByteArrayOutputStream();

byte[] b = new byte[1024];

int n;

while ((n = fis.read(b)) != -1) {

bos.write(b, 0, n);

}

byte[] data = bos.toByteArray();

if (data == null)

return null;

if (config == null) {

return BitmapDecoder.decodeSampledBitmapFromByteArray(data, 0, data.length);

} else {

return BitmapDecoder

.decodeSampledBitmapFromByteArray(data, 0, data.length, config.width, config.height);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (bos != null)

bos.close();

} catch (Exception e) {

e.printStackTrace();

}

try {

if (fis != null)

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return null;

}

/**

* 从磁盘获取bitmap

*

* @param key

* @return

*/

private Bitmap getBitmapFromDiskWithoutLru(String key, BitmapConfig config) {

byte[] data = ImgFileManager.getImage(mContext, key);

if (data == null)

return null;

if (config == null) {

return BitmapDecoder.decodeSampledBitmapFromByteArray(data, 0, data.length);

} else {

return BitmapDecoder.decodeSampledBitmapFromByteArray(data, 0, data.length, config.width, config.height);

}

}

/**

* 将缓存记录同步到journal文件中。

*/

public void fluchCache() {

if (mDiskLruCache != null) {

try {

mDiskLruCache.flush();

} catch (IOException e) {

e.printStackTrace();

}

}

}

public void closeCache() {

if (mDiskLruCache != null) {

try {

mDiskLruCache.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值