android picasso invalidate not working [duplicate]

泄露秘密 提交于 2019-12-24 11:27:48

问题


if(isConnected()) { // if device online
    Picasso.with(context)
            .load(url)
            .networkPolicy(NetworkPolicy.NO_CACHE)
            .placeholder(R.drawable.ic_contact_picture)
            .error(R.drawable.ic_contact_picture)
            .into(imageView, new Callback() {
                @Override
                public void onSuccess() { }
                @Override
                public void onError() {
                    Picasso.with(context).invalidate(url);
                }
            });
} else { // if device offline
    Picasso.with(context)
            .load(url)
            .networkPolicy(NetworkPolicy.OFFLINE) // force load cached image
            .placeholder(R.drawable.ic_contact_picture)
            .error(R.drawable.ic_contact_picture)
            .into(imageView);
}

Earlier I had an image on my server. I fetched that image using picasso and it was cached.

Now I deleted that image, so I want to invalidate that image in picasso cache. Now on refreshing the imageview when device is online, the imageview was blank and onError was executed, as per expected.

BUT,

Now when the device is offline, the else part is executed and since I've given networkpolicy.offline, picasso will be forced to show cached image, which should not exist since I had invalidated it before. Still it shows the image from the cache. Why?


回答1:


use this to invalidate your image path:

Picasso.with(mContext).invalidate(ImagePath + ".jpg");  

and to load your image use:

 Picasso.with(mContext).load(ImagePath+".jpg").networkPolicy(NetworkPolicy.NO_CACHE).placeholder(R.drawable.loading).error(R.drawable.ic_new).into(Imageview);


来源:https://stackoverflow.com/questions/33398792/android-picasso-invalidate-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!