Fresco以编程方式将Bitmap添加到Cache

时间:2017-06-05 10:34:12

标签: android bitmap fresco

我正在将图像上传到我的服务器,一旦上传,我的服务器就会响应新的URI(可以是与旧的相同的URL),我想删除旧的缓存图像并插入新的图像新的URI。

我试着这样做:

// Retrofit2 onResponse

String newImageUri = response.body().getUri();
String oldImageUri = Preferences.getUser().getImageUrl();   

// Remove old image from cache
Fresco.getImagePipeline().evictFromCache(Uri.parse(oldImageUri));               
Fresco.getImagePipeline().evictFromDiskCache(Uri.parse(oldImageUri));
Fresco.getImagePipeline().evictFromMemoryCache(Uri.parse(oldImageUri));
Fresco.getImagePipelineFactory().getMainFileCache().remove(new SimpleCacheKey(oldImageUri));

// Insert new image at new URI
try {
    Fresco.getImagePipelineFactory().getMainFileCache().insert(new SimpleCacheKey(newImageUri), new WriterCallback() {
        @Override
        public void write(OutputStream os) throws IOException {
            os.write(imageData); // byte[] or the new Bitmap
        }
    });
}
catch (Exception e) {
    e.printStackTrace();
}
uriProfileImage.setImageURI(newImageUri);

没有例外,但我仍然只看到旧图像。

2 个答案:

答案 0 :(得分:1)

我解决了它,这是新Uri的Uri格式错误......

答案 1 :(得分:-1)

 Uri uri = Uri.parse("http://frescolib.org/static/fresco-logo.png");
Fresco.getImagePipelineFactory().getMainDiskStorageCache().remove(new CacheKey(uri.toString()));
Fresco.getImagePipelineFactory().getSmallImageDiskStorageCache().remove(new CacheKey(uri.toString()));