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

时间:2016-09-30 12:24:14

标签: java android parse-platform picasso

使用SwipeRefreshLayout刷新时,我执行了以下代码。这应该加载新的配置文件图片,但看起来旧图像以某种方式缓存在设备上。当我退出应用程序并返回活动时,新的个人资料图片可用。但令人耳目一新并不能解决这个问题。我能在这做什么?

滑动

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

            }
        });
    }

从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();
}

我不确定我是如何得到imagePath的,因为我是从互联网上下载图片的。但也许这不是我应该做的事情?

2 个答案:

答案 0 :(得分:2)

这是我的代码,如果该文件的缓存存在,它将首先从缓存加载图像,然后更新缓存文件。如果它不存在,则从互联网(网址)加载。

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

答案 1 :(得分:1)

尝试使用此

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