2016-09-07 74 views
-4

我做了一個應用程序,上傳圖像到服務器,然後根據特定的URL加載它。問題是,如果我知道上傳之前該圖像的確切網址,是否可以緩存而不需要做出新的請求?Android Picasso使手動緩存

回答

0

首先創建一個新的類來獲得自己的單畢加索對象

import android.content.Context; 
import com.squareup.picasso.Downloader; 
import com.squareup.picasso.OkHttpDownloader; 
import com.squareup.picasso.Picasso; 

public class PicassoCache { 

    /** 
    * Static Picasso Instance 
    */ 
    private static Picasso picassoInstance = null; 

    /** 
    * PicassoCache Constructor 
    * 
    * @param context application Context 
    */ 
    private PicassoCache (Context context) { 

     Downloader downloader = new OkHttpDownloader(context, Integer.MAX_VALUE); 
     Picasso.Builder builder = new Picasso.Builder(context); 
      builder.downloader(downloader); 

     picassoInstance = builder.build(); 
    } 

    /** 
    * Get Singleton Picasso Instance 
    * 
    * @param context application Context 
    * @return Picasso instance 
    */ 
    public static Picasso getPicassoInstance (Context context) { 

     if (picassoInstance == null) { 

      new PicassoCache(context); 
      return picassoInstance; 
     } 

     return picassoInstance; 
    } 

} 

用自己的單畢加索對象,而不是Picasso.With()

PicassoCache.getPicassoInstance(getContext()).load(imagePath).into(imageView) 

答案第三個問題:你做不需要任何磁盤許可磁盤緩存操作