滑行图像缓存(ID不包含网址) [英] Glide Image cache with id not url

查看:90
本文介绍了滑行图像缓存(ID不包含网址)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用从Amazon S3加载图像的Android应用程序. 图片网址会随令牌和有效期密钥的变化而随机变化.因此,我无法缓存图像Glide.

I am working in Android app that load images from Amazon S3. The Image URL randomly changes with token and expiry key. For that reason i can't cache the image Glide.

有任何方法可以将Glide缓存键设置为任何静态ID(例如图片ID)而不是网址.

There is any way to set Glide cache key as any static ID(like image id) not url.

我附加了我的代码段以从AWS加载图像

I attached my code snippet to load image from AWS

Glide.with(remoteGalleryAct).load(photoFinalImageURL)
                .signature(new StringSignature(getImageUrl(photoFinalImageURL)))// remove AWS keys
                .error(defaultNoImageDrawable)
                .placeholder(defaultNoImageDrawable)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(new ImageViewTarget<GlideDrawable>(photoHolder.photo) {
                    @Override
                    protected void setResource(GlideDrawable resource) {
                    }

                    @Override
                    public void onResourceReady(final GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        //super.onResourceReady(resource, glideAnimation);
                        view.setImageDrawable(resource);
                    }
                });

请建议我在Glide中有什么方法可以实现.

Please suggest me there is any way to achieve in Glide.

推荐答案

重写GlideUrl类的getCacheKey()方法.此方法设置用于缓存图像的密钥.

Override getCacheKey() method of GlideUrl class. This method sets the key for caching the image.

方法如下:

//Create a custom class and override the method to remove authentication header from the S3 image url

public class GlideUrlCustomCacheKey extends GlideUrl {
    public GlideUrlCustomCacheKey(String url) {
        super(url);
    }

    public GlideUrlCustomCacheKey(String url, Headers headers) {
        super(url, headers);
    }

    public GlideUrlCustomCacheKey(URL url) {
        super(url);
    }

    public GlideUrlCustomCacheKey(URL url, Headers headers) {
        super(url, headers);
    }

    @Override
    public String getCacheKey() {
        String url = toStringUrl();
        if (url.contains("?")) {
            return url.substring(0, url.lastIndexOf("?"));
        } else {
            return url;
        }
    }
}

使用从此类获取的URL设置imageView:

Set the imageView with the URL obtained from this class:

Glide.with(context).load(new GlideUrlCustomCacheKey(buzzUrl)).into(imageView);

这篇关于滑行图像缓存(ID不包含网址)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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