用于将图像上传到android的内存缓存

时间:2013-07-19 04:54:01

标签: android image

有人可以向我解释一下这段代码。我不完全理解这一点。它是为上传图像创建一个内存cashe .. 特别是,我想知道 * 1.什么是softReference * 2.什么是containsKey * 3. softReference是否存储在内存中 * 4.为什么Cashe是synchronizedMap(cache = Collections.synchronizedMap)

非常感谢

package com.androidhive.imagefromurl; 

import java.lang.ref.SoftReference; 
import java.util.Collections; 
import java.util.HashMap; 
import java.util.Map; 
import android.graphics.Bitmap; 

public class MemoryCache { 
private Map<String, SoftReference<Bitmap>> cache=Collections.synchronizedMap(new HashMap<String,   SoftReference<Bitmap>>()); 

public Bitmap get(String id){ 
    if(!cache.containsKey(id)) 
        return null; 
    SoftReference<Bitmap> ref=cache.get(id); 
    return ref.get(); 
} 

public void put(String id, Bitmap bitmap){ 
    cache.put(id, new SoftReference<Bitmap>(bitmap)); 
} 

public void clear() { 
    cache.clear(); 
} 
}

1 个答案:

答案 0 :(得分:0)

我相信你应该首先阅读这些文件,然后带着你的具体问题回到这里。

SoftReference documentation

containsKey method from Map Class

synchronizedMap (Map map) documentation