简体   繁体   English

从Web服务加载数据后刷新时清除Picasso图像缓存

[英]Clear Picasso image cache on refresh after loading data from web service

On refresh with SwipeRefreshLayout, I have the following code executed. 在使用SwipeRefreshLayout刷新时,我执行了以下代码。 This should load the new profile picture but it appears the old image is cached somehow on the device. 这应加载新的个人资料图片,但似乎旧图像以某种方式缓存在设备上。 When I exit the app and return to the activity, the new profile picture is available. 当我退出应用程序并返回活动时,可以使用新的个人资料图片。 But refreshing won't solve this. 但是刷新不会解决这个问题。 What can I do here? 我在这里可以做什么?

Swipe: 滑动:

private void setSwipeRefreshLayout(boolean isOnline, String userId) {
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setOnRefreshListener(() -> {
            if (!isOnline) {
                mSwipeRefreshLayout.setRefreshing(false);
            } else {
                if (mSwipeRefreshLayout.isRefreshing()) {
                    mSwipeRefreshLayout.setRefreshing(false);
                }

                if (userId != null && userId.equals(ParseUser.getCurrentUser().getObjectId())) {
                    createProfileHeader(userId);
                    Picasso.with(getApplicationContext()).invalidate(imagePath);
                }

            }
        });
    }

Retrieve profile picture from Parse: 从Parse检索个人资料照片:

if (headerUserObject.getParseFile("profilePicture") != null) {

    Picasso.with(getApplicationContext())
            .load(headerUserObject.getParseFile("profilePicture").getUrl())
            .placeholder(R.color.placeholderblue)
            .into(((ImageView) findViewById(R.id.profile_picture)));

    fadeInProfilePicture();
}

I'm not sure how I'd get imagePath above, since I'm downloading the image from the internet. 我不确定如何从上面获取imagePath ,因为我是从互联网上下载图像的。 But maybe this is not what I'm supposed to do? 但这也许不是我应该做的吗?

Here is my code which will first load the image from the cache if the cache for that file exists, then update the cache file. 这是我的代码,如果该文件的缓存存在,它将首先从缓存中加载图像,然后更新缓存文件。 If it doesn't exist, then load it from the internet(url). 如果不存在,请从Internet(url)加载它。

Picasso.with(getApplicationContext()).load(headerUserObject.getParseFile("profilePicture").getUrl())
            .placeholder(R.color.placeholderblue).networkPolicy(NetworkPolicy.OFFLINE) // try to load the image from cache first
            .into(((ImageView) findViewById(R.id.profile_picture)), new Callback() {
                        @Override
                        public void onSuccess() {
                            //cache file for this url exists, now update the cache for this url
                            if(networkAvaialable()) // if internet is working update the cache file
                                Picasso.with(getApplicationContext()).invalidate(headerUserObject.getParseFile("profilePicture").getUrl());
                        }

                        @Override
                        public void onError() { // if cache file does not exist load it from the internet
                            Picasso.with(getApplicationContext()).load(headerUserObject.getParseFile("profilePicture").getUrl())
                            .placeholder(R.color.placeholderblue)
                            .into(((ImageView) findViewById(R.id.profile_picture)));
                        }
                    });

试试这个

 Picasso.memoryPolicy(MemoryPolicy.NO_CACHE).into(image);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM