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?

Droidman
  • 11,485
  • 17
  • 93
  • 141
dreambit.io dreambitio
  • 1,892
  • 1
  • 15
  • 24
  • 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? – Stan Apr 21 '15 at 13:43
  • @Stan 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 streaming](http://frescolib.org/docs/progressive-jpegs.html#_) – Droidman Apr 21 '15 at 14:11
  • 1
    1. Yes, we are using progressive JPEG. 2. It is new. All new things are interesting) – dreambit.io dreambitio Apr 21 '15 at 14:20
  • 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. – Stan Apr 21 '15 at 14:27
  • BTW using http://frescolib.org/docs/using-image-pipeline.html#skipping-the-decode we could get the compressed image. Its like a contents of an image file actually. – Stan Apr 21 '15 at 14:43
  • @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 proble – prasanthMurugan Mar 15 '16 at 10:02

5 Answers5

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();
kleopatra
  • 51,061
  • 28
  • 99
  • 211
danhantao
  • 61
  • 1
  • 1
  • 1
    I am getting error: "can not resolve `getMainDiskStorageCache()`" – Nishant Bhakta May 27 '18 at 05:24
  • 1
    Be warned, this approach returns an encoded version of the file in Kotlin with the following code: `ImagePipelineFactory.getInstance().mainFileCache.getResource(cacheKey` – Foobar Jun 03 '18 at 02:24
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.

Jalpesh
  • 1,104
  • 1
  • 13
  • 25
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.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
shuo Han
  • 79
  • 6
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.

abcfrom0
  • 31
  • 2
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
    }
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138