【问题标题】:Check if Image is in Cache - Universal Image Loader检查图像是否在缓存中 - 通用图像加载器
【发布时间】:2013-03-08 10:13:33
【问题描述】:

我想标题说明了一切。我试过了:

imageLoader.getMemoryCache().get(key); 

以图片uri为key,但总是返回null

虽然我在配置中启用了缓存。

【问题讨论】:

    标签: android universal-image-loader


    【解决方案1】:

    使用MemoryCacheUtils

    MemoryCacheUtils.findCachedBitmapsForImageUri(imageUri, ImageLoader.getInstance().getMemoryCache());
    

    内存缓存可以包含一个图像的多个位图(不同大小)。所以内存缓存使用特殊键,而不是图像 url。

    【讨论】:

    • @NOSTRA 这对我不起作用。如果我使用: MemoryCacheUtil.findCachedBitmapsForImageUri(url, ImageLoader.getInstance());它要求我铸造,所以...这是正确的吗? MemoryCacheUtil.findCachedBitmapsForImageUri(url, (MemoryCacheAware) ImageLoader.getInstance());
    • @NOSTRA,为什么使用这个代码 MemoryCacheUtil.findCachedBitmapsForImageUri(imageUri, ImageLoader.getInstance().getMemoryCache());返回位图的arraylist?并获取arraylist 中的第一个值返回随机位图?提前致谢。
    • 因为内存缓存可以包含同一图像的不同大小(位图)。其实你可以在配置中拒绝它。
    • @NOSTRA,你能看看这篇文章吗? stackoverflow.com/questions/26480098/…
    【解决方案2】:

    应该是MemoryCacheUtils,所以应该使用

    MemoryCacheUtils.findCachedBitmapsForImageUri(imageUri, ImageLoader.getInstance().getMemoryCache());
    

    【讨论】:

      【解决方案3】:

      磁盘缓存使用下面的代码

      public static boolean isDiskCache(String url) {
              File file = DiskCacheUtils.findInCache(url, ImageLoader.getInstance().getDiskCache());
              return file != null;
      }
      

      【讨论】:

        【解决方案4】:

        有时,在使用通用图像加载器库时,加载器需要一段时间来验证远程图像是否已加载到缓存中。要直接加载缓存文件,可以使用以下方法检查远程文件的本地副本是否已经生成:

            File file = imageLoader.getDiscCache().get(url);  
         if (!file.exists()) {  
              DisplayImageOptions options = new DisplayImageOptions.Builder()  
              .cacheOnDisc()  
              .build();  
              imageLoader.displayImage(url, imageView, options);  
         }  
         else {  
              imageView.setImageURI(Uri.parse(file.getAbsolutePath()));  
         }  
        

        在这里查看enter link description here

        【讨论】:

          【解决方案5】:

          我认为您可以像这样在实用程序类中创建一个简单的方法:

          public static boolean isImageAvailableInCache(String imageUrl){
              MemoryCache memoryCache = ImageLoader.getInstance().getMemoryCache();
              if(memoryCache!=null) {
                  for (String key : memoryCache.keys()) {
                      if (key.startsWith(imageUrl)) {
                          return true;
                      }
                  }
              }
              return false;
          }
          

          并像这样使用它:

             if(Utils.isImageAvailableInCache(imageUrl)){
          //
                }
          

          【讨论】:

            猜你喜欢
            • 2016-01-05
            • 2011-01-27
            • 1970-01-01
            • 2013-05-02
            • 2015-07-11
            • 1970-01-01
            • 2016-02-29
            • 2014-01-14
            • 2014-10-14
            相关资源
            最近更新 更多