【问题标题】:Fresco not updating disk cache with images from the networkFresco 未使用来自网络的图像更新磁盘缓存
【发布时间】:2015-06-24 15:04:27
【问题描述】:

我一直在尝试使用 Facebook 的 Fresco 库。

从文档中我了解到,当我们使用SimpleDraweeView 时,图像的缓存可以使用ImagePipelines 开箱即用。我几乎看到事情完美地只在轻微的问题上运行,即使 Fresco 从我的 URI 中获取图像,DiskCache 和其他缓存中的图像也不会被它替换。

我对获取图像的代码持肯定态度,因为在我清除缓存后第一次运行应用程序时,我的图像确实会显示出来,并且我实现了 RequestListenerControllerListener,它们都成功了。

这是我目前的设置:

// 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


    【解决方案1】:

    这与此处提出的问题基本相同:https://github.com/facebook/fresco/issues/124

    事实上,您在上面粘贴的两行中关于从磁盘缓存中删除的想法是正确的。你只需要在正确的地方给他们打电话。 onRequestSuccess 不是正确的位置,因为这会破坏您刚刚下载的图像。

    您想在 之前从缓存中删除旧条目,甚至发出新条目的请求。如果 Fresco 在磁盘上找到图像,它甚至不会发出网络请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-18
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 2014-03-31
      • 2016-08-30
      • 1970-01-01
      相关资源
      最近更新 更多