【发布时间】:2015-06-24 15:04:27
【问题描述】:
我一直在尝试使用 Facebook 的 Fresco 库。
从文档中我了解到,当我们使用SimpleDraweeView 时,图像的缓存可以使用ImagePipelines 开箱即用。我几乎看到事情完美地只在轻微的问题上运行,即使 Fresco 从我的 URI 中获取图像,DiskCache 和其他缓存中的图像也不会被它替换。
我对获取图像的代码持肯定态度,因为在我清除缓存后第一次运行应用程序时,我的图像确实会显示出来,并且我实现了 RequestListener 和 ControllerListener,它们都成功了。
这是我目前的设置:
// Setting the collaborator image.
GenericDraweeHierarchy collaboratorPicHierarchy =
genericDraweeHierarchyBuilder
.setPlaceholderImage(
getInitialsAsDrawable(
collaborator.getUser().getInitials()
)
)
.setRoundingParams(new RoundingParams()
.setRoundAsCircle(true)
)
.build();
holder.collaborator.setHierarchy(collaboratorPicHierarchy);
holder.collaborator.setImageURI(
Uri.parse(AltEngine.formURL("user/getProfilePic/") +
accessToken + "/" +
collaborator.getUser().getUuid()
)
);
所有这些代码都位于适配器内部,并且在适配器的构造函数中我已经初始化了 Fresco。
imagePipelineConfig = ImagePipelineConfig.newBuilder(this.context)
.build();
Fresco.initialize(this.context, imagePipelineConfig);
在 StackOverflow 中搜索了很多之后,我发现可以使用以下 Snippet。
Fresco.getImagePipelineFactory().getMainDiskStorageCache().remove(new SimpleCacheKey(uri.toString()));
Fresco.getImagePipelineFactory().getSmallImageDiskStorageCache().remove(new SimpleCacheKey(uri.toString()));
我在与SimpleDraweeView 相关联的ImageRequest 实例的onRequestSuccess 中尝试了该方法,但这导致每次刷新ListView 时都会显示占位符,这很糟糕。
我找到的下一个解决方案来自这里 Android - How to get image file from Fresco disk cache?,这表明可能需要为 Fresco 实现我自己的 DiskCacheConfig 以使我的 DiskCache 无效,所以我尝试使用我在 SO 问题中找到的代码配置我的 DiskCache。
DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder().setBaseDirectoryPath(this.context.getCacheDir())
.setBaseDirectoryName("v1")
.setMaxCacheSize(100 * ByteConstants.MB)
.setMaxCacheSizeOnLowDiskSpace(10 * ByteConstants.MB)
.setMaxCacheSizeOnVeryLowDiskSpace(5 * ByteConstants.MB)
.setVersion(1)
.build();
imagePipelineConfig = ImagePipelineConfig.newBuilder(this.context)
.setMainDiskCacheConfig(diskCacheConfig)
.build();
Fresco.initialize(this.context, imagePipelineConfig);
仍然没有通过网络图像更新缓存。
我不知道这里出了什么问题。可能是我完全弄错了 Fresco。无论如何我被困在这里并且不知道如何继续。我已经阅读了几次文档,由于运气不好,我可能错过了一些重要的东西。请指出正确的方向。
【问题讨论】:
标签: android image-caching fresco