static void clearCache(Context context) { try { getCache(context).clearCache(); } catch (IOException e) { Logger.log(LoggingBehavior.CACHE, Log.WARN, TAG, "clearCache failed " + e.getMessage()); } }
private Bitmap readImage(String uri, boolean expectedFromCache) { Bitmap bmp = null; InputStream istream = null; try { Uri url = Uri.parse(uri); // Check if the cache contains value for this url boolean isInCache = (ImageResponseCache.getCache(safeGetContext()).get(url.toString()) != null); assertTrue(isInCache == expectedFromCache); // Read the image istream = ImageResponseCache.getCachedImageStream(url, safeGetContext()); if (istream == null) { HttpURLConnection connection = (HttpURLConnection) (new URL(uri)).openConnection(); istream = ImageResponseCache.interceptAndCacheImageStream( safeGetContext(), connection); } assertTrue(istream != null); bmp = BitmapFactory.decodeStream(istream); assertTrue(bmp != null); } catch (Exception e) { assertNull(e); } finally { Utility.closeQuietly(istream); } return bmp; }
static InputStream getCachedImageStream(Uri uri, Context context) { InputStream imageStream = null; if (uri != null) { if (isCDNURL(uri)) { try { FileLruCache cache = getCache(context); imageStream = cache.get(uri.toString()); } catch (IOException e) { Logger.log(LoggingBehavior.CACHE, Log.WARN, TAG, e.toString()); } } } return imageStream; }
static InputStream interceptAndCacheImageStream( Context context, HttpURLConnection connection ) throws IOException { InputStream stream = null; if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { Uri uri = Uri.parse(connection.getURL().toString()); stream = connection.getInputStream(); // Default stream in case caching fails try { if (isCDNURL(uri)) { FileLruCache cache = getCache(context); // Wrap stream with a caching stream stream = cache.interceptAndPut( uri.toString(), new BufferedHttpInputStream(stream, connection)); } } catch (IOException e) { // Caching is best effort } } return stream; }
static void clearCache(Context context) { try { getCache(context).clearCache(); } catch (IOException e) { Logger.log(LoggingBehavior.CACHE, Log.WARN, TAG, "clearCache failed " + e.getMessage()); } }
static InputStream getCachedImageStream(URI url, Context context) { InputStream imageStream = null; if (url != null) { if (isCDNURL(url)) { try { FileLruCache cache = getCache(context); imageStream = cache.get(url.toString()); } catch (IOException e) { Logger.log(LoggingBehavior.CACHE, Log.WARN, TAG, e.toString()); } } } return imageStream; }
static InputStream interceptAndCacheImageStream(Context context, HttpURLConnection connection) throws IOException { InputStream stream = null; if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { URL url = connection.getURL(); stream = connection.getInputStream(); // Default stream in case caching fails try { if (isCDNURL(url.toURI())) { FileLruCache cache = getCache(context); // Wrap stream with a caching stream stream = cache.interceptAndPut( url.toString(), new BufferedHttpInputStream(stream, connection)); } } catch (IOException e) { // Caching is best effort } catch (URISyntaxException e) { // Caching is best effort } } return stream; }