/** * 获取图标 * * @param url 图标的地址 * @param callback 异常处理网络事件 GET_ICON, GET_ICON_FAILED * @return */ public Bitmap get(String url, Handler callback) { WeakReference<Bitmap> ref = imageCache.get(url); if (ref != null && ref.get() != null) // 内存有 { return ref.get(); } // 读文件 File iconFile = new File(getIconFullPath(url)); byte[] icondata = null; if (iconFile.exists()) { try { FileInputStream fis = new FileInputStream(iconFile); icondata = Tools.readByteFromInputStream(fis); Bitmap iconTemp = BitmapFactory.decodeByteArray(icondata, 0, icondata.length); put(url, iconTemp); return iconTemp; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } ImageRequest req = new ImageRequest(url, callback); // 读网络 synchronized (locker) { if (iconRequest.contains(req)) // 如果已经发了请求 { return null; } else { iconRequest.add(req); // Log.v("Test", "add" + req.url); } } sendIconRequest(req); return null; }
private String getIconFullPath(String url) { String localPath = Tools.tranferUrltoLocalPath(url); String absolutePath = Tools.getStorePath(ReadApplication.getAppContext(), ICON_PATH); return absolutePath + File.separator + localPath; }