use of com.facebook.cache.common.SimpleCacheKey in project SherlockAdapter by EvilBT.
the class FrescoUtil method save.
public static Boolean save(@NonNull String url, @NonNull File outputFile) throws IOException {
ImagePipelineFactory factory = Fresco.getImagePipelineFactory();
ImagePipeline pipeline = factory.getImagePipeline();
boolean isInCache = pipeline.isInDiskCacheSync(Uri.parse(url));
if (isInCache) {
BinaryResource resource = factory.getMainFileCache().getResource(new SimpleCacheKey(url));
if (resource instanceof FileBinaryResource) {
FileBinaryResource fileResource = (FileBinaryResource) resource;
FileChannel input = new FileInputStream(fileResource.getFile()).getChannel();
FileChannel output = new FileOutputStream(outputFile).getChannel();
output.transferFrom(input, 0, input.size());
input.close();
output.close();
return true;
}
}
boolean isMemoryCache = pipeline.isInBitmapMemoryCache(Uri.parse(url));
if (!isMemoryCache) {
return false;
}
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url)).build();
DataSource<CloseableReference<CloseableImage>> dataSource = pipeline.fetchImageFromBitmapCache(request, null);
if (!dataSource.isFinished()) {
return false;
}
CloseableReference<CloseableImage> closeableImageRef = dataSource.getResult();
Bitmap bitmap = null;
if (closeableImageRef != null && closeableImageRef.get() instanceof CloseableBitmap) {
bitmap = ((CloseableBitmap) closeableImageRef.get()).getUnderlyingBitmap();
}
if (bitmap == null) {
return false;
}
FileOutputStream outputStream = new FileOutputStream(outputFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
return true;
}
Aggregations