我正在使用Volley来缓存应用程序中的图像。内存缓存工作正常,但没有图像缓存在磁盘上。代码如下
VolleySingleton.java
public class VolleySingleton {
public static final String TAG = "VolleySingleton";
private static VolleySingleton mInstance = null;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private VolleySingleton(Context context){
mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(this.mRequestQueue,new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(3);
@Override
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url,bitmap);
}
});
}
public static VolleySingleton getmInstance(Context context){
if(mInstance == null){
mInstance = new VolleySingleton(context);
}
return mInstance;
}
public RequestQueue getmRequestQueue(){
return this.mRequestQueue;
}
public ImageLoader getmImageLoader(){
return mImageLoader;
}
}
图像加载到 CustomAdapter ChannelAdapter.java
private class ChannelAdapter extends BaseAdapter{
private LayoutInflater inflater;
private VolleySingleton volleySingleton;
public ChannelAdapter(Context context) {
super(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
volleySingleton = VolleySingleton.getmInstance(context);
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null){
view = inflater.inflate(R.layout.item,viewGroup,false);
}
NetworkImageView imageView = (NetworkImageView) view.findViewById(R.id.imageView);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(tvChannel.getName());
imageView.setImageUrl(tvChannel.getImages().get(link,volleySingleton.getmImageLoader());
return view;
}
}