Android 如何使用glide库在recyclerview中加载更新的图像而不是旧图像
我实现了Glide库以在ImageView中加载图像,它工作正常 依赖关系:Android 如何使用glide库在recyclerview中加载更新的图像而不是旧图像,android,android-imageview,android-glide,Android,Android Imageview,Android Glide,我实现了Glide库以在ImageView中加载图像,它工作正常 依赖关系: compile "com.github.bumptech.glide:glide:4.5.0" annotationProcessor "com.github.bumptech.glide:compiler:4.5.0" compile "jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0" Glide.with(c
compile "com.github.bumptech.glide:glide:4.5.0"
annotationProcessor "com.github.bumptech.glide:compiler:4.5.0"
compile "jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0"
Glide.with(context)
.load(ImageUrl)
.apply(bitmapTransform(new jp.wasabeef.glide.transformations.BlurTransformation(25)
).diskCacheStrategy(DiskCacheStrategy.ALL))
.apply(new RequestOptions()
.placeholder(R.mipmap.ic_launcher))
.into(view);
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
在适配器中:
compile "com.github.bumptech.glide:glide:4.5.0"
annotationProcessor "com.github.bumptech.glide:compiler:4.5.0"
compile "jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0"
Glide.with(context)
.load(ImageUrl)
.apply(bitmapTransform(new jp.wasabeef.glide.transformations.BlurTransformation(25)
).diskCacheStrategy(DiskCacheStrategy.ALL))
.apply(new RequestOptions()
.placeholder(R.mipmap.ic_launcher))
.into(view);
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
问题是,当我上传图像并在recyclerview中显示,并从API获取URL时,它会显示以前从缓存中添加的图像,而不是新的图像。我只需要在第一个位置添加新图像,而不是刷新整个列表
通过使用以下命令:
compile "com.github.bumptech.glide:glide:4.5.0"
annotationProcessor "com.github.bumptech.glide:compiler:4.5.0"
compile "jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0"
Glide.with(context)
.load(ImageUrl)
.apply(bitmapTransform(new jp.wasabeef.glide.transformations.BlurTransformation(25)
).diskCacheStrategy(DiskCacheStrategy.ALL))
.apply(new RequestOptions()
.placeholder(R.mipmap.ic_launcher))
.into(view);
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
上面的代码刷新整个列表,因为清除了整个缓存数据。我只需要在列表中显示新上传的图像。您需要验证的第一件事是服务器/cdn的响应头或您正在使用的承载图像的任何东西 Glide知道您的图像(来自同一url)是否更新的唯一方法是通过cache/ETAG头 现在棘手的部分是:Glide是一个图像缓存/加载库,它处理内存/磁盘缓存,但不执行实际的网络部分。它由第三方库(如OkHttp)处理,因此您需要扩展glide模块和/或配置网络库,以适合您的应用程序的方式尊重缓存头 以下是有关glide github的问题的链接,将为您指明正确的方向: 请这样设置:-
Glide.with(context)
.load(img_url)
.apply(new RequestOptions()
.placeholder(R.mipmap.personal_pic)
.diskCacheStrategy(DiskCacheStrategy.ALL))
.into(iv_profile_pic);
如何通过添加url或仅通过注册位图加载到图像?请分享你的适配器。我不认为你可以检查特定的url,无论内容是否被更改。因此,解决方案是每次上传图像时使用唯一的url。另一个解决方案是使用图像资源的元数据。您必须利用Glide的
.signature()
功能。也许会有帮助。简单的解决方案是@ADM提到的,每次上传图像时都生成新的url。有没有一个例子说明如何做到这一点?它不起作用。它只是显示磁盘缓存中的旧映像,而不是新映像。请使用.signature(new ObjectKey(file.lastModified())而不是.diskCacheStrategy(diskCacheStrategy.ALL))