首页 > 代码库 > 使用LruCache和DiskLruCache来下载图片

使用LruCache和DiskLruCache来下载图片

LruCache是一个很好用的图片缓存工具:

主要做法是:滑动图片时将图片的bitmap缓存在LruCache<String, Bitmap>中,退出程序后将图片缓存进文件里,采用DiskLruCache mDiskLruCache


所以我们必须设置一个图片缓存的地址:

public void setImageCache(){
    	String strPath = null;
    	if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
	    	File sdFile = Environment.getExternalStorageDirectory();
	    	strPath = sdFile.getAbsolutePath() + "/pic/";
	    	File cacheFile = new File(strPath);
	        if(!cacheFile.exists()){
	        	cacheFile.mkdir();
	        }
	    }
    	else{
    		String strCacheDir = this.getCacheDir().getAbsolutePath();
    		strPath = strCacheDir + "/pic/";
    	}
    	setCachePath(strPath);
    }
	
	private void setCachePath(String strPicCachePath){
		if(TextUtils.isEmpty(strPicCachePath)){
			return;
		}
		m_strPicCachePath = strPicCachePath;
		File cacheFile = new File(strPicCachePath);
        if(!cacheFile.exists()){
        	cacheFile.mkdir();
        }
	}
	
	public String getCacheBmpPath(String strUrl){
		if(TextUtils.isEmpty(m_strPicCachePath) || TextUtils.isEmpty(strUrl)){
			return "";
		}
		return m_strPicCachePath + StringUtil.MD5Encode(strUrl) + mFileExName;//".bmp";
	}

然后写List的adapter:

	private class ListAdapter extends BaseAdapter implements OnScrollListener {

		protected List<ShopData> items = new ArrayList<ShopData>();
		protected static final int FETCH_IMAGE_MSG = 1;
		private LruCache<String, Bitmap> mMemoryCache;
		protected HashSet<ImageView> mItemsMissingImages = new HashSet<ImageView>();
		protected ImageLoaderHandler mHandler;
		protected ImageLoader mImageFetcher;
		public static final int TIMEOUT = 30000;//超时时间30秒
		private DiskLruCache mDiskLruCache;  

		public ListAdapter() {
			super();
			mHandler = new ImageLoaderHandler();
			int maxMemory = (int) Runtime.getRuntime().maxMemory();
			int mCacheSize = maxMemory / 8;
			// 给LruCache分配1/8 4M
			mMemoryCache = new LruCache<String, Bitmap>(mCacheSize) {

				// 必须重写此方法,来测量Bitmap的大小
				@Override
				protected int sizeOf(String key, Bitmap value) {
					return value.getRowBytes() * value.getHeight();
				}

			};
			
		 try {
			mDiskLruCache = DiskLruCache  
			                .open(new File(m_strPicCachePath), getAppVersion(MainActivityTest.this), 1, 10 * 1024 * 1024);
		} catch (IOException e) {
			e.printStackTrace();
		}  

		}
		
		/**
		 * 将缓存记录同步到journal文件中。
		 */
		public void fluchCache() {
			if (mDiskLruCache != null) {
				try {
					mDiskLruCache.flush();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		

		@Override
		public int getCount() {
			return items.size();
		}

		@Override
		public Object getItem(int position) {
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ShopDataTag tag = new ShopDataTag();
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.listitem, null);
				tag.name = (TextView) convertView.findViewById(R.id.name);
				tag.shopInfo = (TextView) convertView.findViewById(R.id.info);
				tag.icon = (ImageView) convertView.findViewById(R.id.image_icon);
				convertView.setTag(tag);
			} else {
				tag = (ShopDataTag) convertView.getTag();
			}

			TextView name = tag.name;
			TextView info = tag.shopInfo;
			ImageView imageView = tag.icon;
			ShopData data = http://www.mamicode.com/items.get(position);>

效果图:

代码:http://download.csdn.net/detail/baidu_nod/7777137