Example #1
0
  public static String getImageSavePath(CachedImageKey key) {
    String filePath = null;
    if (key == null || StringUtil.isEmpty(key.getImageUrl())) {
      return null;
    }

    filePath =
        File.separator + IMAGE_FOLDER[key.getCacheType()] + File.separator + key.getCachedName();
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
      filePath = ImageCache.filePath + filePath;
    } else {
      filePath = ImageCache.secondaryFilePath + filePath;
    }
    return filePath;
  }
  @Override
  protected Bitmap doInBackground(Void... params) {
    if (imageView == null || url == null) {
      return bitmap;
    }

    if (Logger.isDebug()) Log.i(LOG_TAG, "Get Header image from remote!");
    try {
      Bitmap newBitmap = ImageUtil.getBitmapByUrl(url);

      /** 加入cache中* */
      if (newBitmap != null) {
        // 生成mini图
        Bitmap scaleBitmap =
            ImageUtil.scaleBitmapTo(newBitmap, SheJiaoMaoApplication.getSmallAvatarSize());
        Bitmap roundBitmap = ImageUtil.getRoundedCornerBitmap(scaleBitmap);
        if (scaleBitmap != newBitmap) {
          scaleBitmap.recycle();
        }

        CachedImage resultWrap = new CachedImage(roundBitmap);
        if (isMini) {
          bitmap = roundBitmap;
        }
        imageInfo.setCacheType(CachedImageKey.IMAGE_HEAD_MINI);
        imageCache.put(imageInfo, resultWrap);

        // 生成normal图
        scaleBitmap =
            ImageUtil.scaleBitmapTo(newBitmap, SheJiaoMaoApplication.getNormalAvatarSize());
        roundBitmap = ImageUtil.getRoundedCornerBitmap(scaleBitmap);
        scaleBitmap.recycle();

        resultWrap = new CachedImage(roundBitmap);
        imageInfo.setCacheType(CachedImageKey.IMAGE_HEAD_NORMAL);
        imageCache.put(imageInfo, resultWrap);

        newBitmap.recycle();
        if (!isMini) {
          bitmap = roundBitmap;
        }
      }
    } catch (LibException e) {
      if (Logger.isDebug()) Log.e(LOG_TAG, e.getMessage(), e);
    }

    return bitmap;
  }
Example #3
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();
        }
      }
    }
  }
Example #4
0
  public static String getRealPath(CachedImageKey key) {
    String filePath = null;
    if (key == null || StringUtil.isEmpty(key.getImageUrl())) {
      return filePath;
    }

    filePath =
        File.separator + IMAGE_FOLDER[key.getCacheType()] + File.separator + key.getCachedName();
    File file = new File(ImageCache.filePath + filePath);
    if (file.exists() && file.isFile()) {
      return file.getPath();
    }

    file = new File(ImageCache.secondaryFilePath + filePath);
    if (file.exists() && file.isFile()) {
      return file.getPath();
    }

    return null;
  }
Example #5
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);
  }
Example #6
0
  // 把数组写入文件
  public static void write(CachedImageKey key, byte[] imgBytes) {
    if (key == null
        || StringUtil.isEmpty(key.getImageUrl())
        || imgBytes == null
        || imgBytes.length == 0) {
      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;
    }

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

      fos.write(imgBytes);

      fos.flush();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (fos != null) {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
Example #7
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);
    }
  }