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 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); } }
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; }
// 把数组写入文件 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(); } } } }