Search code examples
androidpicasso

Picasso loads wrong images


I have a weird behavior using Picasso. I have three screens. Screen A have a recycler view, when user clicking in one item the Screen B opens with more details about the clicked item. In the screen B a have three image thumbnails and when user click in any of those thumbnails (that I loaded with picasso) I start a background thread to write these bitmaps to files and pass the file paths for screen C. The Screen C has a view pager for showing the images. So, I load the images again with picasso using now, the file path.

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View view = LayoutInflater.from(this.context).inflate(R.layout.item_fullscreen_image,
            container, false);
    ImageView img = (ImageView) view.findViewById(R.id.img);
    //img.setImageURI(Uri.parse(filePaths.get(position)));
    Picasso.with(context).load(new File(filePaths.get(position))).into(img);
    ((ViewGroup) container).addView(photoView);
    return view;
} 

The problem is, when user select a item from Screen A, go to screen B and tap an imageview to open the Screen C with view pager, if is the first time, works fine.

If is not the first time, the images loaded from file with picasso don't change, it loads always the same three images for populate the view pager.

I already tried to use a Target to load, but the problem persists:

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View view = LayoutInflater.from(this.context).inflate(R.layout.item_fullscreen_image,
            container, false);
    ImageView img = (ImageView) view.findViewById(R.id.img);
    Target target = new TargetBitmaps(img);
    img.setTag(target);
    Picasso.with(context).load(new File(filePaths.get(position))).into(target);
    ((ViewGroup) container).addView(photoView);
    return view;
} 

private class TargetBitmaps implements Target {
    private ImageView img;

    public TargetBitmaps(ImageView img) {
        this.img = img;
    }

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        img.setImageBitmap(bitmap);
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {

    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {

    }
}

I know that is not problem with my function to save bitmap to file because if I don't use picasso and do in that way, works fine:

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View view = LayoutInflater.from(this.context).inflate(R.layout.item_fullscreen_image,
            container, false);
    ImageView img = (ImageView) view.findViewById(R.id.img);
    img.setImageURI(Uri.parse(filePaths.get(position)));
    ((ViewGroup) container).addView(photoView);
    return view;
} 

So, if anyone can help me and tell what I'm doing wrong, I appreciate.


Solution

  • I found the solution:

    I do this in the fragment that holds the viewPager:

    @Override
    public void onDestroy() {
        super.onDestroy();
        for (String path : filePaths) {
            Picasso.with(getContext()).invalidate(new File(path));
        }
    }
    

    With this Picasso remove the three files from cache and shows the correct in next time.