文件操作类
public class FileUtil {
private Context mContext;
/**
* 手机sd卡目录
*/
private static final String mRootPath =Environment.getExternalStorageDirectory().getPath();
/**
* 图片缓存目录
*/
private static final String MTEMPIMAGEPATH_STRING = "/TempImage";
/**
* 手机缓存目录
*/
private static String MPHONECACHE_STRING;
public FileUtil(Context context){
this.mContext = context;
MPHONECACHE_STRING = mContext.getCacheDir().getPath();
}
/**
* 判断sd卡是否可用,否则用手机缓存
* @return
*/
private String getPath(){
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
?mRootPath+MTEMPIMAGEPATH_STRING : MPHONECACHE_STRING+MTEMPIMAGEPATH_STRING;
}
private boolean isExistFile(String fileName){
return new File(getPath()+File.separator+fileName).exists();
}
/**
* 保存bitmap到sd卡(或手机缓存)
* @param urlName
* @param bitmap
* @throws IOException
*/
public void saveBitmap(String urlName,Bitmap bitmap) throws IOException{
if(bitmap == null){
return ;
}
String rootPath = getPath();
File rootFile = new File(rootPath);
if(!rootFile.exists()){
rootFile.mkdir();
}
File bitmapFile = new File(rootPath+File.separator+urlName);
bitmapFile.createNewFile();
FileOutputStream fos = new FileOutputStream(bitmapFile);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
/**
* 获取bitmap
* @param urlName
* @return
*/
public Bitmap getBitmapFromDir(String urlName){
if(isExistFile(urlName)){
return null;
}
return BitmapFactory.decodeFile(getPath()+File.separator+urlName);
}
public void deleteBitmapFile(){
String rootPath = getPath();
File rootFile = new File(rootPath);
if(!rootFile.exists()){
return ;
}
if(rootFile.isDirectory()){
File []chilleFile = rootFile.listFiles();
for(File temp : chilleFile){
temp.delete();
}
}
//删除根目录
rootFile.delete();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
核心类(LruCache)
public class BitmapLoadCache {
public static final String TAG = "BitmapLoadCache";
private FileUtil mFileUtil;
private Listener mListener;
private LruCache<String,Bitmap> bitmapLruCache;
private Context mContext;
private final int maxCacheSize = (int)Runtime.getRuntime().maxMemory();//获取应用的最大可用内存
private final int bitmapsCacheSize = maxCacheSize/4;//使用最大可用内存的1/4来缓存bitmap
public static BitmapLoadCache bitmapLoadCache;
public BitmapLoadCache(Context context){
this.mContext = context;
mFileUtil = new FileUtil(mContext);
//初始化lrucache
bitmapLruCache = new LruCache<String, Bitmap>(bitmapsCacheSize){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getAllocationByteCount();
}
@Override
protected void entryRemoved(boolean evicted, String key,
Bitmap oldValue, Bitmap newValue) {
//当规定的内存存储控件不足时,保存在sd卡中
addValueToSDCard(key, oldValue);
}
};
}
public static BitmapLoadCache getbitmapLruCache(Context context){
if(bitmapLoadCache == null){
bitmapLoadCache = new BitmapLoadCache(context);
}
return bitmapLoadCache;
}
/*
* 取出cache中的bitmap
* 根据图片的url
*/
private Bitmap getValueFromCache(String urlKey){
return bitmapLruCache.get(urlKey);
}
/*
* 将图片加载到内存中
*/
public boolean addValueToCache(String urlKey,Bitmap value){
if(getValueFromCache(urlKey) == null && value != null){
bitmapLruCache.put(urlKey, value);
return true;
}
return false;
}
/**
* 外部调用,获取bitmap(1cache,2sd卡,3重现下载)
* @param urlKey
* @return
*/
public Bitmap getBitmap(String urlKey){
if(getValueFromCache(urlKey) != null){
return getValueFromCache(urlKey);
}else if(getValueFromSDCard(urlKey) != null){
Bitmap bitmap = getValueFromSDCard(urlKey);
//重新保存在lrucache
addValueToCache(urlKey, bitmap);
return bitmap;
}else{
return null;
}
}
/**
* 进行下载任务(应该用线程池?)
* @param url
* @return
*/
public Bitmap doDownLoad(final String url){
//这里处理下载任务
Bitmap bitmap = getBitmap(url);
if(bitmap != null){
return bitmap;
}else{
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
//回调,通知主线程下载完成
mListener.onFinish(url, (Bitmap)msg.obj);
}
};
new Thread(new Runnable() {
@Override
public void run() {
Bitmap bitmap = getValueFromUrl(url);
Message message = handler.obtainMessage();
message.obj = bitmap;
handler.sendMessage(message);
try {
mFileUtil.saveBitmap(url, bitmap);
} catch (IOException e) {
Log.e(TAG,e.getMessage());
}
}
}).start();
}
return null;
}
private void addValueToSDCard(String url,Bitmap value){
try {
mFileUtil.saveBitmap(url, value);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 进行网络连接获取图片
* @param urlKey
* @return
*/
private Bitmap getValueFromSDCard(String urlKey){
return mFileUtil.getBitmapFromDir(urlKey);
}
private Bitmap getValueFromUrl(String url){
Bitmap bitmap = null;
HttpURLConnection con = null;
try {
URL mImageUrl = new URL(url);
con = (HttpURLConnection) mImageUrl.openConnection();
con.setConnectTimeout(10 * 1000);
con.setReadTimeout(10 * 1000);
con.setDoInput(true);
con.setDoOutput(true);
bitmap = BitmapFactory.decodeStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
con.disconnect();
}
}
return bitmap;
}
/**
* 暂停下载任务
*/
public void cancelTask(){
}
public interface Listener{
public void onFinish(String urlKey,Bitmap value);
}
public void setListener(Listener listener){
this.mListener = listener;
}
}