用于类似图库,缓存,所困、缩略图
package com.example.cache;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.util.DisplayMetrics;
/**
* 1.从内存中加载
* 2.本地缓存中加载
* 3.本地加载
* @author Administrator
*
*/
public class LoadCacheImageTool {
private Activity activity;
private Map cacheMap;
public LoadCacheImageTool(Activity activity){
this.activity = activity;
this.cacheMap = new HashMap();
}
public Bitmap loadCacheImage(String imagePath){
Bitmap bitmap = null;
if (cacheMap.containsKey(imagePath)) {
bitmap = cacheMap.get(imagePath).get();
if (bitmap!=null) {
return bitmap;
}
}
bitmap = loadLocalCacheImage(imagePath);
cacheMap.put(imagePath, new SoftReference(bitmap));
return bitmap;
}
///mnt/sdcard/bk.png
///mnt/sdcard/cache/bk.png.cache
private Bitmap loadLocalCacheImage(String imagePath) {
Bitmap bitmap = null;
String cacheImagePath = getCacheImagePath(imagePath);
File cacheFile = new File(cacheImagePath);
if (!cacheFile.exists()) {
bitmap = loadLocalBigImage(imagePath);
saveToCacheDir(bitmap,cacheImagePath);
}else{
try {
bitmap = BitmapFactory.decodeStream(new FileInputStream(cacheFile));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bitmap;
}
private String getCacheImagePath(String imagePath) {
String cacheDir = new File(imagePath).getParent()+"/cache/";
if (!new File(cacheDir).exists()) {
new File(cacheDir).mkdirs();
}
String newImageName = new File(imagePath).getName()+".cache";
String newImagePath = cacheDir+newImageNam
2021-11-03 21:55:51
2.04MB
图片缓存
1