Search in sources :

Example 1 with ImagePipelineFactory

use of com.facebook.imagepipeline.core.ImagePipelineFactory 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;
}
Also used : ImagePipelineFactory(com.facebook.imagepipeline.core.ImagePipelineFactory) FileChannel(java.nio.channels.FileChannel) CloseableReference(com.facebook.common.references.CloseableReference) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) FileInputStream(java.io.FileInputStream) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) CloseableBitmap(com.facebook.imagepipeline.image.CloseableBitmap) Bitmap(android.graphics.Bitmap) BinaryResource(com.facebook.binaryresource.BinaryResource) FileBinaryResource(com.facebook.binaryresource.FileBinaryResource) FileOutputStream(java.io.FileOutputStream) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) SimpleCacheKey(com.facebook.cache.common.SimpleCacheKey) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline)

Aggregations

Bitmap (android.graphics.Bitmap)1 BinaryResource (com.facebook.binaryresource.BinaryResource)1 FileBinaryResource (com.facebook.binaryresource.FileBinaryResource)1 SimpleCacheKey (com.facebook.cache.common.SimpleCacheKey)1 CloseableReference (com.facebook.common.references.CloseableReference)1 ImagePipeline (com.facebook.imagepipeline.core.ImagePipeline)1 ImagePipelineFactory (com.facebook.imagepipeline.core.ImagePipelineFactory)1 CloseableBitmap (com.facebook.imagepipeline.image.CloseableBitmap)1 CloseableImage (com.facebook.imagepipeline.image.CloseableImage)1 ImageRequest (com.facebook.imagepipeline.request.ImageRequest)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileChannel (java.nio.channels.FileChannel)1