Volley Cache无法正常工作

时间:2017-01-20 22:01:59

标签: android caching android-volley

我正在尝试将下载的图像缓存在主视图上,并在详细视图中显示它们。我测试的方式如下:首先,我在gridview上显示所有图像并断开Internet连接,并希望在细节上看到缓存的图像。但是,在细节活动中,图像未显示。

我使用以下 Volley Singleton 课程如下:

public class CustomVolleyRequest {

    private static CustomVolleyRequest customVolleyRequest;
    private static Context context;
    private RequestQueue requestQueue;
    private ImageLoader imageLoader;

    private CustomVolleyRequest(Context context) {
        CustomVolleyRequest.context = context;
        this.requestQueue = getRequestQueue();

        imageLoader = new ImageLoader(requestQueue,
                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap>
                            cache = new LruCache<>(20);

                    @Override
                    public Bitmap getBitmap(String url) {
                        return cache.get(url);
                    }

                    @Override
                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);
                    }
                });
    }

    public static synchronized CustomVolleyRequest getInstance(Context context) {
        if (customVolleyRequest == null) {
            customVolleyRequest = new CustomVolleyRequest(context);
        }
        return customVolleyRequest;
    }

    public RequestQueue getRequestQueue() {
        if (requestQueue == null) {
            requestQueue = Volley.newRequestQueue(context.getApplicationContext(), 10 * 1024 * 1024);
        }
        return requestQueue;
    }

    ImageLoader getImageLoader() {
        return imageLoader;
    }
}

主要活动适配器

public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
    final ViewHolder holder;
    if(convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
    }
  holder.imageView.setImageUrl(imageURL, mImageLoader);
}

详情活动

// I have checked and confirmed that the following url is same with the one the main activity
String url = getArguments().getString("image_url");
ImageLoader imageLoader = CustomVolleyRequest.getInstance(getContext()).getImageLoader();
mImageView.setImageUrl(url, imageLoader);

1 个答案:

答案 0 :(得分:2)

我认为Volley缓存仍然有效,但是,您的问题是因为详细活动中的NetworkImageView与主要活动中的NetworkImageView不同,它还需要加载图像成功一次成功。在此处,您将在开始详细活动之前断开Internet连接。

您可以在NetworkImageView.java

中看到逻辑

注意#122

中的行
// if there was an old request in this view, check if it needs to be canceled.
        if (mImageContainer != null && mImageContainer.getRequestUrl() != null) {...

和#134

// The pre-existing content of this view didn't match the current URL. Load the new image
        // from the network.
        ImageContainer newContainer = mImageLoader.get(mUrl,...

更新2017/02/10

NetworkImageView内部详细信息活动从网络(ImageContainer newContainer = mImageLoader.get...)加载新图片时,ImageLoader.java内的方法get将被调用。

参见代码:

...
final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);
// Try to look up the request in the cache of remote images.
Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
if (cachedBitmap != null) {
    // Return the cached bitmap.
    ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
    imageListener.onResponse(container, true);
    return container;
}
...

你会发现,如果两个活动中的图像视图具有相同的宽度和高度,那么cacheKey也将是相同的,cachedBitmap将不为空,将使用缓存的位图对于NetworkImageView内部细节活动也是如此。