简体   繁体   English

Android - 如何从Fresco磁盘缓存中获取映像文件?

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

I am using the Fresco library. 我正在使用Fresco库。

I can't find any related info in the Fresco documentation, how can I get an image file from Fresco's disk cache? 我在Fresco文档中找不到任何相关信息,如何从Fresco的磁盘缓存中获取图像文件?

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 检查Plamenkos在此链接上的答案.. 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. 如果您要求管道获取编码图像,并使用ImageRequestBuilder.setLowestPermittedRequestLevel指定DISK_CACHE,则管道将返回JPEG字节(如果在磁盘缓存中找到图像),如果找不到图像则为null,并且完全取出必须要执行。

I hope I did not mis-understand your question and provide a wrong answer. 我希望我没有误解你的问题并提供错误的答案。

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. 我认为你永远不应该试图获得Fresco Cache名称因为缓存是内部工具。

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. 这个方法返回的速度非常快,你可以在UI线程中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM