Skip to content

Instantly share code, notes, and snippets.

@chethann
Created November 28, 2016 15:59
Show Gist options
  • Save chethann/87373c319e2b9713b0d3ef21424f5a92 to your computer and use it in GitHub Desktop.
Save chethann/87373c319e2b9713b0d3ef21424f5a92 to your computer and use it in GitHub Desktop.
Code to get InputStream of an image synchronously in Fresco
public static InputStream readFromCacheSync(String imageUrl) {
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(ImageRequest.fromUri(imageUrl), null);
StagingArea stagingArea = StagingArea.getInstance();
EncodedImage encodedImage = stagingArea.get(cacheKey);
if (encodedImage != null) {
return encodedImage.getInputStream();
}
try {
return readFromDiskCache(cacheKey);
} catch (Exception e) {
return null;
}
}
private static InputStream readFromDiskCache(final CacheKey key) throws IOException {
try {
FileCache fileCache = ImagePipelineFactory.getInstance().getMainFileCache();
final BinaryResource diskCacheResource = fileCache.getResource(key);
if (diskCacheResource == null) {
FLog.v(TAG, "Disk cache miss for %s", key.toString());
return null;
}
PooledByteBuffer byteBuffer;
final InputStream is = diskCacheResource.openStream();
FLog.v(TAG, "Successful read from disk cache for %s", key.toString());
return is;
} catch (IOException ioe) {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment