Android - How to get image file from Fresco disk cache?
Asked Answered
V

5

12

I am using the Fresco library.

I can't find any related info in the Fresco documentation, how can I get an image file from Fresco's disk cache?

Vanadinite answered 21/4, 2015 at 12:59 Comment(6)
While I can't answer your question Im curious why you are using this fresh new lib instead of Glide or Picasso which are documented good enough?Transistor
@Transistor I'm also interested in an answer since I decided to try that lib. Why? 'Cause it offers some interesting features Picasso doesn't have, such as progressive JPEG streamingSienkiewicz
1. Yes, we are using progressive JPEG. 2. It is new. All new things are interesting)Vanadinite
Well, actually its interesting for me too since it has some native implemetations like JPEG/PNG decoders and other features. However I'd tried it vs UIL in one project and with UIL it has better perfomance in ListView when scrolling. With Glide, Picasso and Fresco List lags while it scrolls but not with UIL. Must say that none of this libs has a normal way to get the image file from its cache. Its always some tricky way and for UIL too.Transistor
BTW using frescolib.org/docs/… we could get the compressed image. Its like a contents of an image file actually.Transistor
@EldarMensutov hello iam having the same problem iam using fresco in the list view .I dont know how to maintain the disk cache in fresco suggest me an example help me out to solve the probleEradis
C
6

if the image have download in cache,you can do it like:

ImageRequest imageRequest=ImageRequest.fromUri(url);
CacheKey cacheKey=DefaultCacheKeyFactory.getInstance()
     .getEncodedCacheKey(imageRequest);
BinaryResource resource = ImagePipelineFactory.getInstance()
     .getMainDiskStorageCache().getResource(cacheKey);
File file=((FileBinaryResource)resource).getFile();
Cautery answered 24/7, 2015 at 12:18 Comment(2)
I am getting error: "can not resolve getMainDiskStorageCache()"Oilskin
Be warned, this approach returns an encoded version of the file in Kotlin with the following code: ImagePipelineFactory.getInstance().mainFileCache.getResource(cacheKeyStuyvesant
S
2

I hope this helps

Check Plamenkos answer on this link.. https://github.com/facebook/fresco/issues/80

if you ask pipeline for an encoded image, and specify DISK_CACHE with ImageRequestBuilder.setLowestPermittedRequestLevel, the pipeline will return you the JPEG bytes if the image is found anywhere up to disk cache, or null if the image is not found and a full-fetch would have to be performed.

I hope I did not mis-understand your question and provide a wrong answer.

Starstarboard answered 28/4, 2015 at 7:57 Comment(0)
S
2
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(YourImageUrl))
            .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.DISK_CACHE)
            .setResizeOptions(new ResizeOptions(width, width))
            .build();
DraweeController controller = Fresco.newDraweeControllerBuilder()
            .setOldController(YourImageView.getController())
            .setImageRequest(request)
            .build(); 

This code:

.setLowestPermittedRequestLevel(ImageRequest.RequestLevel.DISK_CACHE)

will make fresco get your image from the disk first and then the net.

Shama answered 17/6, 2016 at 8:56 Comment(1)
Sorry, for a minute there I messed up your answer. But it helped me understand, so thanks!Aliphatic
R
1

I think you should never try to get Fresco Cache name cause cache is the internal implement.

But if you want to know whether a image has been cached, you can use this:

private boolean isDownloaded(Uri loadUri) {
    if (loadUri == null) {
        return false;
    }
    ImageRequest imageRequest = ImageRequest.fromUri(loadUri);
    CacheKey cacheKey = DefaultCacheKeyFactory.getInstance()
            .getEncodedCacheKey(imageRequest);
    return ImagePipelineFactory.getInstance()
            .getMainDiskStorageCache().hasKey(cacheKey);
}

this method is return very fast, you can use it in UI thread.

Renoir answered 26/7, 2015 at 11:14 Comment(1)
Faster than '.setLowestPermittedRequestLevel(ImageRequest.RequestLevel.DISK_CACHE)' ??Camiecamila
D
0
 private fun getGitFile(gifUrl: String): File {
        val request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(gifUrl))
                .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.DISK_CACHE)
                .build()
        val cacheKey = DefaultCacheKeyFactory.getInstance()
                .getEncodedCacheKey(request, null)
        val resource = ImagePipelineFactory.getInstance()
                .mainFileCache.getResource(cacheKey)
        val file = (resource as FileBinaryResource).file
        return file
    }
Dilantin answered 23/9, 2019 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.