/**
  * 删除缓存的图片
  *
  * @param url 缓存的图片的地址
  */
 public static void removeMemoryCache(String url) {
   if (TextUtils.isEmpty(url)) {
     return;
   }
   DiskCacheUtils.removeFromCache(url, ImageLoader.getInstance().getDiskCache());
   MemoryCacheUtils.removeFromCache(url, ImageLoader.getInstance().getMemoryCache());
 }
示例#2
0
  /** 从ImageLoader内存缓存中取出头像位图 */
  private static Bitmap getMemoryCachedAvatarBitmap(UserInfoProvider.UserInfo userInfo) {
    if (userInfo == null || !isImageUriValid(userInfo.getAvatar())) {
      return null;
    }

    String key = HeadImageView.getAvatarCacheKey(userInfo.getAvatar());

    // DiskCacheUtils.findInCache(uri, ImageLoader.getInstance().getDiskCache() 查询磁盘缓存示例
    List<Bitmap> bitmaps =
        MemoryCacheUtils.findCachedBitmapsForImageUri(
            key, ImageLoader.getInstance().getMemoryCache());
    if (bitmaps.size() > 0) {
      return bitmaps.get(0);
    }

    return null;
  }
 private void initEmptyFieldsWithDefaultValues()
 {
     if (taskExecutor == null)
     {
         taskExecutor = DefaultConfigurationFactory.createExecutor(threadPoolSize, threadPriority, tasksProcessingType);
     } else
     {
         customExecutor = true;
     }
     if (taskExecutorForCachedImages == null)
     {
         taskExecutorForCachedImages = DefaultConfigurationFactory.createExecutor(threadPoolSize, threadPriority, tasksProcessingType);
     } else
     {
         customExecutorForCachedImages = true;
     }
     if (diskCache == null)
     {
         if (diskCacheFileNameGenerator == null)
         {
             diskCacheFileNameGenerator = DefaultConfigurationFactory.createFileNameGenerator();
         }
         diskCache = DefaultConfigurationFactory.createDiskCache(context, diskCacheFileNameGenerator, diskCacheSize, diskCacheFileCount);
     }
     if (memoryCache == null)
     {
         memoryCache = DefaultConfigurationFactory.createMemoryCache(memoryCacheSize);
     }
     if (denyCacheImageMultipleSizesInMemory)
     {
         memoryCache = new FuzzyKeyMemoryCache(memoryCache, MemoryCacheUtils.createFuzzyKeyComparator());
     }
     if (downloader == null)
     {
         downloader = DefaultConfigurationFactory.createImageDownloader(context);
     }
     if (decoder == null)
     {
         decoder = DefaultConfigurationFactory.createImageDecoder(writeLogs);
     }
     if (defaultDisplayImageOptions == null)
     {
         defaultDisplayImageOptions = DisplayImageOptions.createSimple();
     }
 }
  /**
   * Adds display image task to execution pool. Image will be set to ImageAware when it's turn.<br>
   * <b>NOTE:</b> {@link #init(ImageLoaderConfiguration)} method must be called before this method
   * call
   *
   * @param uri Image URI (i.e. "http://site.com/image.png", "file:///mnt/sdcard/image.png")
   * @param imageAware {@linkplain com.nostra13.universalimageloader.core.imageaware.ImageAware
   *     Image aware view} which should display image
   * @param options {@linkplain com.nostra13.universalimageloader.core.DisplayImageOptions Options}
   *     for image decoding and displaying. If <b>null</b> - default display image options
   *     {@linkplain
   *     ImageLoaderConfiguration.Builder#defaultDisplayImageOptions(DisplayImageOptions) from
   *     configuration} will be used.
   * @param listener {@linkplain ImageLoadingListener Listener} for image loading process. Listener
   *     fires events on UI thread if this method is called on UI thread.
   * @param progressListener {@linkplain
   *     com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener Listener} for
   *     image loading progress. Listener fires events on UI thread if this method is called on UI
   *     thread. Caching on disk should be enabled in {@linkplain
   *     com.nostra13.universalimageloader.core.DisplayImageOptions options} to make this listener
   *     work.
   * @throws IllegalStateException if {@link #init(ImageLoaderConfiguration)} method wasn't called
   *     before
   * @throws IllegalArgumentException if passed <b>imageAware</b> is null
   */
  public void displayImage(
      String uri,
      ImageAware imageAware,
      DisplayImageOptions options,
      ImageLoadingListener listener,
      ImageLoadingProgressListener progressListener) {
    checkConfiguration();
    if (imageAware == null) {
      throw new IllegalArgumentException(ERROR_WRONG_ARGUMENTS);
    }
    if (listener == null) {
      listener = emptyListener;
    }
    if (options == null) {
      options = configuration.defaultDisplayImageOptions;
    }

    if (TextUtils.isEmpty(uri)) {
      engine.cancelDisplayTaskFor(imageAware);
      listener.onLoadingStarted(uri, imageAware.getWrappedView());
      if (options.shouldShowImageForEmptyUri()) {
        imageAware.setImageDrawable(options.getImageForEmptyUri(configuration.resources));
      } else {
        imageAware.setImageDrawable(null);
      }
      listener.onLoadingComplete(uri, imageAware.getWrappedView(), null);
      return;
    }

    ImageSize targetSize =
        ImageSizeUtils.defineTargetSizeForView(imageAware, configuration.getMaxImageSize());
    String memoryCacheKey = MemoryCacheUtils.generateKey(uri, targetSize);
    engine.prepareDisplayTaskFor(imageAware, memoryCacheKey);

    listener.onLoadingStarted(uri, imageAware.getWrappedView());

    Bitmap bmp = configuration.memoryCache.get(memoryCacheKey);
    if (bmp != null && !bmp.isRecycled()) {
      L.d(LOG_LOAD_IMAGE_FROM_MEMORY_CACHE, memoryCacheKey);

      if (options.shouldPostProcess()) {
        ImageLoadingInfo imageLoadingInfo =
            new ImageLoadingInfo(
                uri,
                imageAware,
                targetSize,
                memoryCacheKey,
                options,
                listener,
                progressListener,
                engine.getLockForUri(uri));
        ProcessAndDisplayImageTask displayTask =
            new ProcessAndDisplayImageTask(engine, bmp, imageLoadingInfo, defineHandler(options));
        if (options.isSyncLoading()) {
          displayTask.run();
        } else {
          engine.submit(displayTask);
        }
      } else {
        options.getDisplayer().display(bmp, imageAware, LoadedFrom.MEMORY_CACHE);
        listener.onLoadingComplete(uri, imageAware.getWrappedView(), bmp);
      }
    } else {
      if (options.shouldShowImageOnLoading()) {
        imageAware.setImageDrawable(options.getImageOnLoading(configuration.resources));
      } else if (options.isResetViewBeforeLoading()) {
        imageAware.setImageDrawable(null);
      }

      ImageLoadingInfo imageLoadingInfo =
          new ImageLoadingInfo(
              uri,
              imageAware,
              targetSize,
              memoryCacheKey,
              options,
              listener,
              progressListener,
              engine.getLockForUri(uri));
      LoadAndDisplayImageTask displayTask =
          new LoadAndDisplayImageTask(engine, imageLoadingInfo, defineHandler(options));
      if (options.isSyncLoading()) {
        displayTask.run();
      } else {
        engine.submit(displayTask);
      }
    }
  }