2015-07-13 97 views
0

我使用畢加索從網上下載圖像,並將它們顯示在RecyclerView中。畢加索內存/磁盤緩存

private Picasso createPicasso(Context context){ 

     OkHttpClient picassoClient = new OkHttpClient(); 

     picassoClient.interceptors().add(new Interceptor() { 
      @Override 
      public Response intercept(Interceptor.Chain chain) throws IOException { 

       try { 

        Map authHeaders = BackendServiceClient.buildAuthHeaders(); 

        Request newRequest = chain.request().newBuilder() 
          .addHeader("Authorization", (String) authHeaders.get("Authorization")) 
          .build(); 
        return chain.proceed(newRequest); 

       } catch (CredentialNotStoredException e) { 
        e.printStackTrace(); 
       } 
       return chain.proceed(chain.request().newBuilder().build()); 
      } 
     }); 

     return new Picasso.Builder(context) 
       .downloader(new OkHttpDownloader(picassoClient)) 
       .build(); 
    } 

用法:

ImageDownloader.getSharedInstance().getPicasso(context) 
      .load(url) 
      .placeholder(R.drawable.head_big) // 
      .error(R.drawable.head_big) // 
      .tag(context) 
      .into(holder.personPhoto); 

下載和顯示圖像按預期工作,但如果我在列表中滾動,圖像被從網絡再次得到,並沒有緩存。 怎麼可能總是緩存他們在磁盤和內存。

+0

....服務器端有支持緩存... – Selvin

+0

沒有辦法迫使緩存磁盤上的圖像? – seeya

+1

帶有正確關鍵字的第一次谷歌搜索會返回此庫答案的創建者......永遠不要低估Google搜索的力量 – Selvin

回答