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

回眸只為那壹抹淺笑 提交于 2019-12-02 22:24:26
danhantao

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();

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.

shuo Han
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.

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.

 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
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!