我正在尝试将下载的图像缓存在主视图上,并在详细 View 中显示它们。我测试的方式如下:首先,我在 gridview 上显示所有图像并断开 Internet 并希望在细节上查看缓存图像。但是,在详细信息 Activity 中未显示图像。
我正在使用以下 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;
}
}
主要 Activity 适配器
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);
}
详细 Activity
// 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);
最佳答案
我认为 Volley 缓存仍然有效,但是,这里的问题是因为详细 Activity 中的 NetworkImageView
与主要 Activity 中的 NetworkImageView
不同,它还需要先成功加载一次图像。在这里,您在开始详细 Activity 之前断开 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
inside detail Activity 从网络加载新图像时 (ImageContainer newContainer = mImageLoader.get...
),方法 get
里面ImageLoader.java将被调用。
查看代码:
...
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;
}
...
你会发现,如果两个 Activity 中的 ImageView 具有相同的宽度和高度,那么 cacheKey
也会相同,并且 cachedBitmap
将不为空,缓存的位图也将用于 NetworkImageView
inside detail Activity 。
关于android - Volley 缓存不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41773059/