9

I am uploading an image to my server and once uploaded my server responds with the new URI for it (can be the same URL as the old one), I want to remove the old cached image and insert the new one for the new URI.

I try to accomplish this by doing:

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

There are no exceptions but I still only see the old image.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Arbitur
  • 38,684
  • 22
  • 91
  • 128
  • In [this](https://stackoverflow.com/a/30256786/2910520) answer they have used the .remove() method calling them from `getImagePipelineFactory().[from_to_delete].remove(...)` while you are using the evict ones. You could try with this and see if helps (maybe will not work for you because the answer is 2 years old and Fresco library probably has changed something in the meantime) – MatPag Jun 05 '17 at 10:38
  • @MatPag I did see that answer, clearly it doesnt work for me. – Arbitur Jun 05 '17 at 10:52
  • You said you only see the old image. How did you test this? – Tin Tran Jun 12 '17 at 17:44

2 Answers2

1

I solved it, it was an error with the Uri format for the new Uri...

Arbitur
  • 38,684
  • 22
  • 91
  • 128
  • 1
    Another worthwhile note: If you are loading ImageView within a RecyclerView.ViewHolder -> you need to ensure the first line you call in OnBindViewHolder is to set imageview source to null. This ensures the image is not reused from the pool. – apelsoczi Jun 12 '17 at 18:53
-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())); 
Al Sweigart
  • 11,566
  • 10
  • 64
  • 92