java Android - 如何从 Fresco 磁盘缓存中获取图像文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29772949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - How to get image file from Fresco disk cache?
提问by Eldar Miensutov
回答by 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();
回答by Jalpesh
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.
我希望我没有误解你的问题并提供错误的答案。
回答by 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)
.setLowestPermittedRequestLevel(ImageRequest.RequestLevel.DISK_CACHE)
will make fresco get your image from the disk first and then the net.
将使 fresco 首先从磁盘获取您的图像,然后是网络。
回答by abcfrom0
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线程中使用它。
回答by NickUnuchek
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
}