3

I have a screen with an ImageView containing the actual profile picture. I can edit that profile picture either by taking a picture with the camera or by picking a picture from the sd card. I store the new chosen profile picture under the same path as the old (i overwrite it) which is logical i guess.

However when i set a new profile picture to my ImageView it does not get refreshed. I have to restart the App to see the change.

this.imageView.invalidate();

is what all people are telling me to do when i brows google but no, this is not working! So how can i force my imageview to load the new profile picture?

I load the image into the ImageView with help of Picasso:

Picasso picasso = Picasso.with(context);

if(reload) {
    picasso.invalidate(new File(fileName));
}

RequestCreator requestCreator = picasso.load(new File(fileName));
requestCreator.into(imageView);
Mulgard
  • 9,877
  • 34
  • 129
  • 232
  • Just load the bitmap again and set it to the `ImageView`. There is no other way to refresh the `ImageView`. – Xaver Kapeller Mar 01 '15 at 16:02
  • that is what im doing with help of picasso. – Mulgard Mar 01 '15 at 16:04
  • You should have mentioned from the beginning that you are using Picasso. Picasso caches images. So what most likely happens it that Picasso loads the old picture from the cache instead of the new picture from the disk. You need to invalidate the cache like this: `Picasso.with(getActivity()).invalidate(file);` – Xaver Kapeller Mar 01 '15 at 16:06
  • 1
    This is also not reloading the new content of the imageview – Mulgard Mar 01 '15 at 16:09
  • It works for me, see my answer. And drop that `if(reload) { ... }`, you don't need that. – Xaver Kapeller Mar 01 '15 at 16:14
  • possible duplicate of [Invalidate cache in Picasso](http://stackoverflow.com/questions/22016382/invalidate-cache-in-picasso) – Xaver Kapeller Mar 01 '15 at 16:24

2 Answers2

7

Picasso caches loaded images to speed up the loading process. This means that if you are trying to reload the same exact picture it loads the cached version and it does not read the changed File from the disk.

You need to tell Picasso that the picture has changed and that it needs to reload the picture from the disk by invalidating the cache. The way you do this is like this:

File file = new File(fileName);
Picasso picasso = Picasso.with(context);
picasso.invalidate(file);
picasso.load(file).into(imageView);
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
4

To skip Picasso's cache, you can use .memoryPolicy(MemoryPolicy.NO_CACHE) static method:

Picasso
    .with(context)
    .load(file)
    .memoryPolicy(MemoryPolicy.NO_CACHE)
    .into(imageView);
Andrew Vovk
  • 353
  • 5
  • 16