예제 #1
0
  @Override
  public CachedImage get(CachedImageKey key) {
    boolean isContain = containsKey(key);
    if (!isContain) {
      return null;
    }

    CachedImage cachedImage = memoryCache.get(key);
    if (cachedImage != null && cachedImage.getWrap() != null) {
      if (Logger.isDebug()) Log.v("ImageCache", "hit memory cache");
      return cachedImage;
    }

    CachedImage temp = read(key);
    Bitmap bitmap = null;
    if (temp != null) {
      bitmap = temp.getWrap();
      if (cachedImage != null) {
        cachedImage.setWrap(bitmap);
      } else {
        cachedImage = temp;
        memoryCache.put(key, cachedImage);
      }
      if (Logger.isDebug()) Log.v("ImageCache", "hit local cache");
    }

    return cachedImage;
  }
예제 #2
0
  @Override
  public void put(CachedImageKey key, CachedImage value) {
    if (value == null || key == null || StringUtil.isEmpty(key.getImageUrl())) {
      return;
    }

    Bitmap bitmap = value.getWrap();
    if (bitmap != null && !value.isLocalCached()) {
      write(key, value);
      value.setLocalCached(true);
    }

    memoryCache.put(key, value);
    if (memoryCache.size() > 50) {
      reclaim(ReclaimLevel.LIGHT);
    }
  }
예제 #3
0
  public CachedImage getMemoryCached(CachedImageKey key) {
    boolean isContain = containsKey(key);
    CachedImage cachedImage = null;
    if (!isContain) {
      return cachedImage;
    }

    cachedImage = memoryCache.get(key);
    if (cachedImage != null && cachedImage.getWrap() != null) {
      if (Logger.isDebug()) Log.v("ImageCache", "getMemoryCached hit memory cache");
      return cachedImage;
    } else {
      cachedImage = null;
    }

    return cachedImage;
  }
예제 #4
0
  @Override
  public void write(CachedImageKey key, CachedImage value) {
    if (value == null) {
      return;
    }

    Bitmap bitmap = value.getWrap();
    if (bitmap == null) {
      return;
    }

    String fileName =
        File.separator + IMAGE_FOLDER[key.getCacheType()] + File.separator + key.getCachedName();
    File file = new File(filePath + fileName);
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
      file = new File(secondaryFilePath + fileName);
    }
    if (file.exists()) {
      return;
    }
    if (Logger.isDebug())
      Log.v(TAG, file.getPath() + "|media state: " + Environment.getExternalStorageState());

    FileOutputStream fos = null;
    try {
      file.createNewFile();
      fos = new FileOutputStream(file);

      bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);

      fos.flush();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (fos != null) {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
예제 #5
0
  public ImageLoad4HeadTask(ImageView imageView, String url, boolean isMini) {
    this.imageView = imageView;
    this.url = url;
    this.isMini = isMini;

    imageCache = (ImageCache) CacheManager.getInstance().getCache(ImageCache.class.getName());
    if (StringUtil.isNotEmpty(url)) {
      if (isMini) {
        imageInfo = new CachedImageKey(url, CachedImageKey.IMAGE_HEAD_MINI);
      } else {
        imageInfo = new CachedImageKey(url, CachedImageKey.IMAGE_HEAD_NORMAL);
      }
      if ((wrap = imageCache.get(imageInfo)) != null) {
        bitmap = wrap.getWrap();
        isHit = true;
      }
    }
  }
예제 #6
0
  public void stat() {
    int imageCount = 0;
    int headCount = 0;
    int thumbnailCount = 0;
    int bigCount = 0;
    int memorySize = 0;

    for (CachedImageKey info : memoryCache.keySet()) {
      CachedImage wrap = memoryCache.get(info);
      imageCount++;
      if (wrap == null) {
        continue;
      }
      Bitmap bitmap = wrap.getWrap();
      if (bitmap == null) {
        continue;
      }
      if (info.getCacheType() == CachedImageKey.IMAGE_HEAD_MINI
          || info.getCacheType() == CachedImageKey.IMAGE_HEAD_NORMAL) {
        headCount++;
      } else if (info.getCacheType() == CachedImageKey.IMAGE_THUMBNAIL) {
        thumbnailCount++;
      } else {
        bigCount++;
      }
      memorySize += bitmap.getWidth() * bitmap.getHeight() * 4;
    }

    System.out.println(
        "ImageCache stat->"
            + ", imageCount:"
            + imageCount
            + ", headCount:"
            + headCount
            + ", thumbnailCount:"
            + thumbnailCount
            + ", bigCount:"
            + bigCount
            + ", memorySize:"
            + memorySize);
  }
예제 #7
0
  @Override
  public boolean reclaim(ReclaimLevel level) {
    switch (level) {
      case LIGHT:
        Set<CachedImageKey> set = memoryCache.keySet();
        Object[] infoArray = null;
        try {
          infoArray = (Object[]) set.toArray();
        } catch (Exception e) {
        }

        if (infoArray == null) {
          break;
        }

        int size = infoArray.length;
        Object temp;
        CachedImageKey key;
        for (int i = 0; i < size; i++) {
          temp = infoArray[i];
          if (temp == null || !(temp instanceof CachedImageKey)) {
            continue;
          }
          key = (CachedImageKey) temp;
          CachedImage wrap = memoryCache.get(key);
          if (wrap == null || wrap.getWrap() == null) {
            memoryCache.remove(key);
          }
        }
        break;
      case MODERATE:
      case WEIGHT:
        memoryCache.clear();
        break;
    }

    return true;
  }