2016-09-30 60 views
0

使用SwipeRefreshLayout進行刷新時,我執行了以下代碼。這應該加載新的配置文件圖片,但它看起來舊的圖像緩存在設備上。當我退出應用程序並返回活動時,新的個人資料圖片可用。但清爽並不能解決這個問題。我能在這裏做什麼?從Web服務加載數據後清除Picasso圖像緩存

刷卡:

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

這裏是我的代碼,將首先從緩存加載圖像,如果該文件的緩存中,然後更新緩存文件。如果它不存在,則從互聯網(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))); 
         } 
        }); 
+0

我認爲''Network.OFFLINE''應該是''NetworkPolicy.OFFLINE'',否則解決我的問題。乾杯。 – santafebound

+0

是啊,對不起,我只是輸入簡單的記事本,所以沒有編輯器來尋找我的錯誤。 –

1

嘗試使用此

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