2
我正在使用畢加索。使用磁盤上的畢加索加載圖像
我想緩存磁盤上的圖像,我正在使用okhttpdownloader。
這是我正在嘗試做的操作我希望Image可以緩存在外部SD卡或手機內存中,但不希望由於內存限制而將圖像緩存在堆內存上。因此,如果第一次加載圖像,它應該被緩存在磁盤上,並且當下一次對相同的url進行請求時,如果它被緩存,它應該首先查看磁盤。
我不能實現這個以下是我的代碼。
public class PicassoCache {
/**
* Static Picasso Instance
*/
private static Picasso picassoInstance = null;
private File cacheDir;
/**
* PicassoCache Constructor
*
* @param context application Context
*/
private PicassoCache (Context context) {
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir = context.getExternalCacheDir();
else
cacheDir = context.getCacheDir();
Downloader downloader = new OkHttpDownloader(cacheDir, 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;
}
}
和內部適配器我有下面的代碼
PicassoCache.getPicassoInstance(context)
.load(item.imageUrl)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(holder.itemImage);
這個工作,但我仍看到有被使用的堆內存時我增加向上和向下滾動。 – apk
我不是堆空間消耗方面的專家,但是由於從磁盤到內存的位圖加載,臨時增加可能是正常行爲。據我所知,最大的問題是**只有當和如果**堆大小在中等時間不穩定,也許你有內存泄漏,也許垃圾回收無效釋放內存。 – GPack