Created
November 28, 2016 15:59
-
-
Save chethann/87373c319e2b9713b0d3ef21424f5a92 to your computer and use it in GitHub Desktop.
Code to get InputStream of an image synchronously in Fresco
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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