我知道 Volley 应该让下载和缓存图像变得简单,但我已经努力了几个小时才能正确实现它。我浏览过网络以及 stackoverflow 上有关 volley 的许多文章,但我发现的示例似乎都不适合我。
我只想使用 volley 从给定的 url 下载和缓存图像,而不是执行任何 HTTP JSON REST 处理。只需要获取给定的 url,下载位图并将它们设置到 imageview,然后将它们添加到缓存中。
这是迄今为止我的最新尝试。
如何正确加载和缓存volley图像?
if (data.getImageUrl() != null) {
try {
holder.thumbnail.setTag(data.getImageUrl());
Cache cache = ImgController.getInstance().getRequestQueue().getCache();
Cache.Entry entry = cache.get(data.getImageUrl());
if (entry != null) {
try {
String cImg = new String(entry.data, "UTF-8");
LruBitmapCache bitmapCache = new LruBitmapCache();
holder.thumbnail.setImageBitmap(bitmapCache.getBitmap(cImg));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
ImageLoader imageLoader = ImgController.getInstance().getImageLoader();
imageLoader.get(data.getImageUrl(), new ImageListener() {
@Override
public void onErrorResponse(VolleyError error) {
holder.thumbnail.setImageResource(R.drawable.filler_icon);
}
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean arg1) {
if (response.getBitmap() != null) {
// load image into imageview
holder.thumbnail.setImageBitmap(response.getBitmap());
}
}
});
}
return convertView;
} catch (Exception e) {
e.printStackTrace();
Log.v(DEBUG_TAG, "no image: ", e);
holder.thumbnail.setImageResource(R.drawable.filler_icon);
}
}else {
return null;
}
return convertView;
}
当我运行这个时,我得到一个指向这一行的 NullPointerException
Cache cache = ImgController.getInstance().getRequestQueue().getCache();
我设置了以下单例类来处理请求
public class ImgController extends Application {
public static final String TAG = ImgController.class.getSimpleName();
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private static ImgController instance;
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
public static synchronized ImgController getInstance(){
return instance;
}
public RequestQueue getRequestQueue(){
if(requestQueue == null){
requestQueue = Volley.newRequestQueue(getApplicationContext());
}
return this.requestQueue;
}
public ImageLoader getImageLoader(){
getRequestQueue();
if(imageLoader == null){
imageLoader = new ImageLoader(this.requestQueue, new LruBitmapCache());
}
return this.imageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (requestQueue != null) {
requestQueue.cancelAll(tag);
}
}
}
以及以下 LruBitmapCache 类
public class LruBitmapCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {
public static int getDefaultLruCacheSize(){
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
return cacheSize;
}
public LruBitmapCache() {
this(getDefaultLruCacheSize());
}
public LruBitmapCache(int maxSize) {
super(maxSize);
}
@Override
public Bitmap getBitmap(String url) {
return get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
put (url, bitmap);
}
}