Android Glide不更新相同url的图像?

Android Glide不更新相同url的图像?,android,caching,imageview,android-glide,Android,Caching,Imageview,Android Glide,我有一张图片的相同的url。当我多次更新此图像时,它会显示上一个图像。服务器上的图像和图片版本已更新,但Glide未显示新图像。我希望每次都获取新图像并将其缓存 Glide.with(context) .load(Constants.COPY_LINK_BASE_URL + info.getDisplayPicture()) .placeholder(R.drawable.ic_profile).dontAnimate() .diskCacheSt

我有一张图片的相同的url。当我多次更新此图像时,它会显示上一个图像。服务器上的图像和图片版本已更新,但Glide未显示新图像。我希望每次都获取新图像并将其缓存

Glide.with(context)
        .load(Constants.COPY_LINK_BASE_URL + info.getDisplayPicture())
        .placeholder(R.drawable.ic_profile).dontAnimate()
        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
        .signature(new (SettingManager.getUserPictureVersion(context)))
        .into(ivUserProfilePhoto);
我可以通过改变互联网复制这个错误,在一个互联网上,它的改变图像与在另一个互联网上预期的一样,在2或3次尝试改变图像后,它保持不变

TL;博士
  • 可以通过添加以下行跳过缓存:

    Glide.with(context)
            // ... (your usual stuff)
            // then this
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            .signature(new StringSignature(String.valueOf(System.currentTimeMillis())));
    
  • :您可以通过向
    URL添加一个伪随机参数来解决缓存问题

    Glide.with(context)
            .load(url + "?rand=" + (new Random().nextInt()))
            .into(imageView);
    
  • 发生了什么事? 第一次加载图片时,它会本地存储在所谓的缓存内存(或简称“缓存”)中。当您第二次请求它时,Glide将从缓存中获取if,就像它是一个成功的请求一样。这意味着有很多好的理由,例如:卸载服务器、为用户保存一些数据和快速响应(为用户提供流畅的体验)

    怎么办? 现在,关于您的问题:您需要禁用缓存,以便在每次请求时强制Glide远程获取您的图像。您可以执行以下操作:

    Glide.with(context)
             .load(url)
             .diskCacheStrategy(DiskCacheStrategy.NONE)
             .skipMemoryCache(true)
             .signature(imageVersion) // supposing that each new image has its own version number
             .into(imageView);
    
    或者,如果您不知道图片何时更改(无
    imageVersion
    ),请为每张图片使用唯一的签名

    Glide.with(context)
             .load(url)
             .signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
             .into(imageView);
    
    另一种干净的方法是在服务器中配置图片的缓存策略,并使用
    .diskCacheStrategy(diskCacheStrategy.SOURCE)

    值得一提的恶作剧 如果向目标URL添加一个伪GET参数并没有破坏任何东西,那么您可以通过在每次获取图像时传递一个随机参数来绕过缓存,这将使Glide认为您正在查询一个新的端点,因此它不会检查缓存

    Glide.with(context)
             .load(url + "?rand=" + (new Random().nextInt())) // "&rand=" if your url already has some GET params
             .into(imageView);
    


    Glide缓存API。

    这是一个老问题,所以我相信您现在已经解决了。然而,我相信两个大图书馆(毕加索和格莱德)现在都支持类似的东西

    Glide具有您可能希望了解的签名API:

    我自己还没有机会处理这个问题,但是看起来您需要调用API来获取版本元数据,然后再调用来实现它

    或者,研究ETag


    祝你好运

    加载或更新新图像时,请使用下面的命令清除滑动内存和缓存:

    |==|清除滑动记忆

     Glide.get(getApplicationContext()).clearMemory();
    
    |==|清除滑动缓存()


    滑动4.x

    Glide.with(context)
         .load(imageUrl)
         .apply(RequestOptions.skipMemoryCacheOf(true))
         .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
         .into(imageView);
    
    Glide.with(context)
         .load(imageUrl)
         .skipMemoryCache(true)
         .diskCacheStrategy(DiskCacheStrategy.NONE)
         .into(imageView);
    
    滑翔3.x

    Glide.with(context)
         .load(imageUrl)
         .apply(RequestOptions.skipMemoryCacheOf(true))
         .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
         .into(imageView);
    
    Glide.with(context)
         .load(imageUrl)
         .skipMemoryCache(true)
         .diskCacheStrategy(DiskCacheStrategy.NONE)
         .into(imageView);
    

    现在,滑动最新版本,您必须使用
    RequestOption
    清除缓存

    RequestOptions options = new RequestOptions()
    .diskCacheStrategy(DiskCacheStrategy.NONE)
    .skipMemoryCache(true);
    
    或者每次加载图片时,必须在as
    签名中使用字符串

    RequestOptions options = new RequestOptions()
    .signature(new StringSignature(String.valueOf(System.currentTimeMillis())));
    
    最后:

    Glide.with(CompressSingleActivity.this)
    .applyDefaultRequestOptions(options)
    .load(currentFile)
    .into(currentImageView);
    

    我的案例无法识别
    .signature()
    ,因此除了@MGDavies之外,您可能还需要类似的东西

    // in kotlin
    Glide.with(context)
            .load("url")
            //using apply to RequestOptions instead signature directly
            .apply(RequestOptions().signature(ObjectKey("time that image was updated")))
            .into(thumb)
    

    就像这个问题与Glide库本身有关,我找到了解决这个问题的方法,只是在你的图片URL后面放一个随机数作为查询,就像下面的示例代码一样,它解决了我的问题,我希望它也能帮助你

            Random random = new Random();
            int randomInt = random.nextInt();
            Glide.with(context)
                    .applyDefaultRequestOptions(new RequestOptions()
                            .circleCrop().diskCacheStrategy(DiskCacheStrategy.NONE))
                    .load(ConstantVars.BaseUrl + userModel.getAvatarURL() + "?" + randomInt)
                    .apply(new RequestOptions().error(R.drawable.himart_place_holder))
                    .into(imageViewProfile);
    

    它将使Glide在您请求时随时重新加载图像,就像根本没有缓存可读取一样。

    Glide 4.8.0

    Glide.with(mContext).asBitmap()
                        .load(nopicUrl)
                        .apply(RequestOptions.skipMemoryCacheOf(true))
                        .apply(RequestOptions.signatureOf(new ObjectKey(String.valueOf(System.currentTimeMillis()))))
                        .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.DATA))
                        .into(ivImageView);
    

    尝试使用RequestOptions:

    RequestOptions requestOptions = new RequestOptions();
    requestOptions.placeholder(R.drawable.ic_placeholder);
    requestOptions.error(R.drawable.ic_error);
    
    Glide.with(context)
         .setDefaultRequestOptions(requestOptions)
         .load(url).into(holder.imageView);
    
    Glide.with(MainActivity.this)
                .load(url)
                .apply(requestOptions)
                .into(imageview);
     // or this
     Glide.with(MainActivity.this)
                .load(url)
                .apply(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
                .into(imageview);
    
     // or this
     Glide.with(MainActivity.this)
                .load(url)
                .apply(RequestOptions.placeholderOf(R.drawable.booked_circle).error(R.drawable.))
                .into(imageview);
    
    如果.setDefaultRequestOptions(requestOptions)不起作用,请使用.apply(requestOptions):


    diskCacheStrategy
    skipMemoryCache
    对我的案例不起作用

    我正在使用相同的url在Glide请求之间写入本地图像文件。我的解决方案是在文件名末尾添加0/1之间切换


    每次强制它更改图像源似乎都有效。

    正在显示缓存的图像。创建自己的磁盘缓存机制,并在服务器上有新映像可用时清除旧映像。检查这可能有助于重复他的问题,他没有使用签名api,我已经在使用它由于要求我不能每次加载图片如果你想保留缓存,你可以添加一些额外的代码到你的服务器,看看图像是否已更改。如果它确实改变了,那么你将清除缓存并加载更新的缓存,如果不只是从缓存中获取它的话。签名来自服务器,告诉url上的图像改变了。这个解决方案对我有效。我要做的唯一更改是使用不同的对象作为签名“ObjectKey(System.currentTimeMillis().toString())”而不是建议的。如果您使用的是Glide版本4或更高版本,这会有所帮助。感谢您的回复,但这并不能完全满足我的要求。如果您想每次加载图像,您需要每次都从服务器更改url。另一个明智的滑动加载相同的图像,这是第一个,如果URL没有得到改变。问题是URL不能被改变的问题是,你的照片将每次加载,这可能会消耗时间,在某些情况下,图像太大,甚至会导致超时。也许在SharedReferences中保存一个布尔值是一个好主意,以说明何时应该重新加载uri。关闭缓存是一个非常糟糕的解决方案。工作非常完美!!为了只清除单个图像的整个缓存,这似乎是非常浪费的。很高兴知道,但这不是针对这个特定问题的推荐解决方案
    Glide.with(MainActivity.this)
                .load(url)
                .apply(requestOptions)
                .into(imageview);
     // or this
     Glide.with(MainActivity.this)
                .load(url)
                .apply(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
                .into(imageview);
    
     // or this
     Glide.with(MainActivity.this)
                .load(url)
                .apply(RequestOptions.placeholderOf(R.drawable.booked_circle).error(R.drawable.))
                .into(imageview);