如何使用Volley缓存NetworkImage在另一个活动中使用 [英] How to cache a NetworkImage to use in Another Activity using Volley

查看:75
本文介绍了如何使用Volley缓存NetworkImage在另一个活动中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种观点,一种是主要观点,另一种是细节观点.我从服务器获取所有网址,并在gridview上相应地显示位图.用户单击网格项目后,我想从主视图中使用缓存的图像.但是,我的以下实现不起作用.

I have two views, one is main and the other one detail. I am getting all the urls from the server and display the bitmap accordingly on the gridview. Once users clicks on a grid item, I want to use cache'd image from the main view. However, my following implementation does not work.

我测试的方法如下:首先,我在gridview上显示所有图像,并断开Internet连接,并希望查看详细信息上的缓存图像.但是,在详细信息活动中,没有显示图像.

The way I am testing is as follows: first, I display all images on the gridview and disconnect the Internet and wanted to see cached image on the detail. However, on the detail activity the image is not being displayed.

我正在使用以下 Volley Singleton 类,如下所示:

I am using the following Volley Singleton class as follows:

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(imageRecord.getUrl(), mImageLoader);
}

详细活动

String url = getArguments().getString("image_url");
ImageLoader imageLoader = CustomVolleyRequest.getInstance(getContext()).getImageLoader();
mImageView.setImageUrl(url, imageLoader);

推荐答案

如果ImageView的大小不同,则图像缓存的默认实现将不起作用,因为ImageLoader传递的缓存的键包括视图的大小:

If the size of the ImageView is different the default implementation for image caching will not work as the key of the caches passed by the ImageLoader include the size of the view:

private static String getCacheKey(String url, int maxWidth, int maxHeight, ScaleType scaleType) {
    return "#W" + maxWidth + "#H" + maxHeight + "#S" + scaleType.ordinal() + url;
}

您有2个主要选择:

1)重做ImageLoader,可能还要重新放置NetworkImageView,因为它会放置实际转换后的位图,该位图的大小可能会有所不同,您可能需要调用createScaledBitmap()将其调整为适合视图的位置.

1) Rework the ImageLoader and probably NetworkImageView as it places the actual converted bitmap, which might be different size and you may want to call createScaledBitmap() to adjust it to the view.

2)确保已缓存从网络接收的原始图像数据.

2) make sure the raw image data received form the network is cached.

所以1)相对复杂,因为您必须更改一些类来改变在ImageRequest中完成的解析,而不是缩放原始图像数据,而是在接收和缓存图像之后执行缩放,基本上是在NetworkImageView中或在ImageLoader的后期进行

So 1) relatively complex because you have to change a few classes chnage the parsing done in ImageRequest and not to scale the raw image data but perform the scaling after the image is received and cached, basically in NetworkImageView or late in ImageLoader

如果您使用2,则在服务器不发送缓存头或etag的情况下,可能需要调整Volley的默认网络缓存实现. 在这种情况下,您需要对ImageLoader进行一些更改,以便可以检查 https://stackoverflow.com/a/36549815/2267924

If you go for 2 you would probably need to adjust the default Network Cache implementation of Volley if you server does not send cache headers or etag. In that case you will need a little change in ImageLoader for that you can check https://stackoverflow.com/a/36549815/2267924

这篇关于如何使用Volley缓存NetworkImage在另一个活动中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆