Code example for WeakHashMap

Methods: containsKey, get, put

0
    public Bitmap fetchDrawable(String urlString) {
    	// 一个主动的fetchDrawable过来后,需要把这个url从cancel列表中清除    	 
    	cancelList.remove(urlString);
    	 
    	// 第一次查缓存 
        if (drawableMap.containsKey(urlString)) {
            return drawableMap.get(urlString);
        } 
         
        Log.d(this.getClass().getSimpleName(), "image url:" + urlString);
        try { 
        	 
        	// 下面这一步可能是很耗时的,在这其间,什么都有可能发生 
            InputStream is = fetch(urlString);
            if(cancelList.contains(urlString))
            	return null; 
             
            // 第二次查缓存 
            if (drawableMap.containsKey(urlString)) {
                return drawableMap.get(urlString);
            } 
             
            // 开始做decode 
            BitmapFactory.Options opt =new BitmapFactory.Options();
            opt.inPreferredConfig = Bitmap.Config.RGB_565;   
            opt.inSampleSize = 1;   //width,hight设为原来的一分之一
            opt.inPurgeable = true; 
            opt.inInputShareable = true;            
            // 下面这一句其实才是OOM的根本所在,尽量避免无意义的decode 
            Bitmap bmp =BitmapFactory.decodeStream(is,null, opt);
             
//            Drawable drawable = Drawable.createFromStream(is, "src"); 
//            if (drawable != null) {            	 
//                drawableMap.put(urlString, drawable); 
//                Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", " 
//                        + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", " 
//                        + drawable.getMinimumHeight() + "," + drawable.getMinimumWidth()); 
//            } else { 
//              Log.w(this.getClass().getSimpleName(), "could not get thumbnail"); 
//            } 
             
            if(bmp != null)
            	drawableMap.put(urlString, bmp);
            return bmp;
        } catch (MalformedURLException e) {
            Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
            return null; 
        } catch (IOException e) {