Android - How to get image file from Fresco disk c

2019-03-11 06:58发布

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?

4条回答
迷人小祖宗
2楼-- · 2019-03-11 07:14

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.

查看更多
Juvenile、少年°
3楼-- · 2019-03-11 07:24

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();
查看更多
该账号已被封号
4楼-- · 2019-03-11 07:28

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.

查看更多
混吃等死
5楼-- · 2019-03-11 07:30
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.

查看更多
登录 后发表回答