【问题标题】:Delete cache while using glide使用滑行时删除缓存
【发布时间】:2017-06-20 20:58:51
【问题描述】:

我知道这是一个非常基本的问题。我试图找到众多解决方案,但我无法理解它们。

我想要什么

将图像上传到服务器,作为回报,我得到了 URL,但问题是在使用此 URL 设置图像时,设置了旧图像。发生这种情况是因为滑翔正在使用旧缓存而不是更新缓存。

如何解决这个问题。

Glide.clear(profilePic);

Glide.with(getApplicationContext())
    .load(url)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .skipMemoryCache(true)
    .transform(new CircleTransform(MainProfile.this))
    .into(profilePic);

目前,图片已更改,但是当我单击后退按钮并返回此活动时,它会加载旧图像。像这样从缓存中加载图像。

//setting up the profile pic
Glide.with(getApplicationContext())
.load(userProfilePicUrl)
.asBitmap()
.centerCrop()
.into(new BitmapImageViewTarget(profilePic) {
    @Override
    protected void setResource(Bitmap resource) {
        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(MainProfile.this.getResources(), resource);
        circularBitmapDrawable.setCircular(true);

        profilePic.setImageDrawable(circularBitmapDrawable);
    }
});

问题是当我回到这个活动时,它显示的是旧照片而不是新照片。

【问题讨论】:

    标签: java android caching android-glide


    【解决方案1】:

    RequestOptions 提供与类型无关的选项,以便在最新版本的 Glide 中使用 Glide 自定义加载。

    制作一个 RequestOptions 对象并在我们加载图像时使用它。

    RequestOptions requestOptions = new RequestOptions()
        .diskCacheStrategy(DiskCacheStrategy.NONE) // because file name is always same
        .skipMemoryCache(true);
    
    Glide.with(this)
        .load(photoUrl)
        .apply(requestOptions)
        .into(profile_image);
    

    【讨论】:

    • 完美运行。
    【解决方案2】:

    试试这个

    Glide.with(DemoActivity.this)
    .load(Uri.parse("file://" + imagePath))
    .diskCacheStrategy(DiskCacheStrategy.NONE)
    .skipMemoryCache(true)
    .into(mImage);
    

    DiskCacheStrategy.ALL 替换为DiskCacheStrategy.NONE

    【讨论】:

    • 没有帮助。当我回来时,它显示的是旧图像@android_griezmann
    • 在这种情况下,这将每次都获取图像。如果互联网不可用,则不会显示图像。
    • @AnkurKhandelwal 这是正确的,据我所知,你不能同时实现两者。
    • 我认为我们可以实现这一点,大多数离线工作的应用程序只有在图像发生更改时才获取图像并显示新图像。如果互联网不可用,则显示新的缓存图像。
    • 嘿,你可能是对的。可以办到。我遇到了this link 找到Custom cache invalidation
    【解决方案3】:

    也许你可以试试这个:

    Glide.get(context).clearDiskCache()
    

    Try reading this link as a ref

    然而,这个解决方案似乎也提供了更多。

    Glide.with(Activity.this)
    .load(Uri)
    .diskCacheStrategy(DiskCacheStrategy.NONE)
    .skipMemoryCache(true)
    .into(Image);
    

    参考之前问过的类似问题,可以找到here

    希望能有所帮助。

    干杯

    【讨论】:

      【解决方案4】:

      Glide 内置了使缓存失效的功能。通过使用signature() 功能可以使旧缓存失效。

      GlideApp.with(MainProfile.this)
      .load(mediaStoreUri)
      .signature(new MediaStoreSignature(mimeType, dateModified, orientation))
      .into(view);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-02
        • 1970-01-01
        • 1970-01-01
        • 2010-12-13
        相关资源
        最近更新 更多