【问题标题】:Image not changing in ImageViewImageView 中的图像没有变化
【发布时间】:2024-01-02 03:04:01
【问题描述】:

我正在 android 中为 imageLoading 制作演示,我首先使用了 UniversalImageLoader 然后 picasso 然后是 ImageLoader 类,但是当我将新图像上传到服务器并成功上传到服务器时当我尝试将其设置为 ImageView 时,之前加载的图像保持不变,它没有改变,我对此感到非常沮丧,浪费了 2 天时间来解决这个问题,但没有运气,请帮助我挽救我的生命。

我的代码如下:

Picasso.with(getApplicationContext())
            .load(Pref.getValue(getApplicationContext(),
                    Const.PREF_PROFILE_PIC, "")).into(iv_profile);

    Picasso.with(getApplicationContext())
            .load(Pref.getValue(getApplicationContext(),
                    Const.PREF_COVER_PIC, "")).into(iv_cover);

【问题讨论】:

  • Picasso、UniversalImageLoader 或任何其他图像加载库正在使用图像缓存来重新加载图像。所以要么您需要更改新图像的名称或清除图像缓存以加载新图像。

标签: android universal-image-loader picasso


【解决方案1】:

尝试在网址末尾添加cachebreaker

String imageurl1 = Const.PREF_PROFILE_PIC + "?" + new Date().getTime();
String imageurl2 = Const.PREF_COVER_PIC + "?" + new Date().getTime();

Picasso.with(getApplicationContext())
        .load(Pref.getValue(getApplicationContext(),
                imageurl1, "")).into(iv_profile);

Picasso.with(getApplicationContext())
        .load(Pref.getValue(getApplicationContext(),
                imageurl2, "")).into(iv_cover);

这将在您创建图像时自动附加当前时间戳,并使浏览器再次查找图像而不是检索缓存中的图像。

【讨论】:

  • 感谢 neeraj 的回复,但我是从服务器获取此图像 url,所以我无法更改 url。还有其他解决方案吗?请。我会等待您的回复
  • 不能修改图片网址吗?
  • 哦,我的天啊...你真是个天才...!!非常感谢,您救了我的命...Neeraj 上帝保佑..!!!
  • 我今天学到了一些东西。谢谢cachebreaker/cache buster@KNeerajLal
【解决方案2】:

思路同上。但它对我没有用。

我从这个link得到了答案

如下所示更改您的网址:

String imageurl = url + "?time=" + System.currentTimeMillis();
Picasso.with(getContext()).load(imageurl).into(imageView);

这对我有用。谢谢

【讨论】: