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

倾然丶 夕夏残阳落幕 提交于 2019-12-03 07:36:30

问题


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?


回答1:


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



回答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.




回答3:


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.




回答4:


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.




回答5:


 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
    }


来源:https://stackoverflow.com/questions/29772949/android-how-to-get-image-file-from-fresco-disk-cache

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