[英]Clear Cache memory of Picasso
我試圖通過Android編碼清除Picasso的緩存。
任何人都可以幫我解決這個問題..?
我嘗試使用以下代碼,但這在我的情況下沒用:
Picasso.with(getActivity()).load(data.get(pos).getFeed_thumb_image()).skipMemoryCache().into(image);
請改用:
Picasso.with(getContext()).load(data.get(pos).getFeed_thumb_image()).memoryPolicy(MemoryPolicy.NO_CACHE).into(image);
像這樣刪除Picasso的緩存。
public class Clear {
public static void clearCache (Picasso p) {
p.cache.clear();
}
}
此util類可以為您清除緩存。 你只需要調用它:
Clear.clearCache(Picasso.with(context));
編輯 :
Clear類必須在包中:
package com.squareup.picasso;
因為無法從該包外部訪問緩存。 就像在這個答案: https : //stackoverflow.com/a/23544650/4585226
如果您嘗試通過Json(從db)加載圖像,請嘗試清除networkCache以獲得更好的結果。
Picasso.with(context).load(uri).networkPolicy(NetworkPolicy.NO_CACHE)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.placeholder(R.drawable.bv_logo_default).stableKey(id)
.into(viewImage_imageView);
當活動破壞時,遺憾的是如果我們使用Picasso,則位圖不會被回收。 我嘗試以編程方式回收位圖,加載到圖像視圖中的內容。 有一種方法可以使用Target
引用加載的位圖。
Target mBackgroundTarget = new Target() {
Bitmap mBitmap;
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
if (bitmap == null || bitmap.isRecycled())
return;
mBitmap = bitmap;
mBgImage.setImageBitmap(bitmap);
mHandler.post(new Runnable() {
@Override
public void run() {
// Do some animation
}
});
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
recycle();
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
/**
* Recycle bitmap to free memory
*/
private void recycle() {
if (mBitmap != null && !mBitmap.isRecycled()) {
mBitmap.recycle();
mBitmap = null;
System.gc();
}
}
};
當Activity破壞時,我調用onBitmapFailed(null)
來回收加載的位圖。
@Override
protected void onDestroy() {
super.onDestroy();
try {
if (mBackgroundTarget != null) {
mBackgroundTarget.onBitmapFailed(null);
Picasso.with(context).cancelRequest(mBackgroundTarget);
}
} catch (Exception e) {
e.printStackTrace();
}
}
但請記住,在這種情況下,請勿在內存中緩存圖像,否則將導致使用回收的位圖異常。
Picasso.with(context)
.load(imageUrl)
.resize(width, height)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.into(mBackgroundTarget);
希望這有幫助。
如果想要使用給定的Uri刷新圖像,而不是清除完整的緩存。 試試這個Picasso.with(context).invalidate(uri);
它在內部從Picasso維護的緩存中刪除密鑰。
摘自Picasso.java
/** * Invalidate all memory cached images for the specified {@code uri}. * * @see #invalidate(String) * @see #invalidate(File) */ public void invalidate(Uri uri) { if (uri == null) { throw new IllegalArgumentException("uri == null"); } cache.clearKeyUri(uri.toString()); }
/** * Invalidate all memory cached images for the specified {@code uri}. * * @see #invalidate(String) * @see #invalidate(File) */ public void invalidate(Uri uri) { if (uri == null) { throw new IllegalArgumentException("uri == null"); } cache.clearKeyUri(uri.toString()); }
如果繼續引用自定義 Downloader
實現,則可以清除緩存。
public class PicassoUtil {
private static Picasso sInstance;
private static OkHttp22Downloader sDownloader;
public static Picasso getPicasso(Context context){
if(sInstance == null) {
sDownloader = new OkHttp22Downloader(context)
Picasso.Builder builder = new Picasso.Builder(context);
builder.downloader(sDownloader);
sInstance = builder.build(sDownloader);
}
return sInstance;
}
public static void clearCache(){
if(sDownloader != null){
sDownloader.clearCache();
}
}
}
訪問您的http客戶端及其Cache
非常重要。 在我的實現中,可以訪問緩存,因此使用clearCache()
方法清除緩存。
我有同樣的問題。 它對我有用。 我在對話框中的RecycleView中使用了Picasso 。 當我關閉對話框時,畢加索不會清除緩存。 但是當您使用該對話框時,它會清除圖像緩存 。 但是有一些緩存未清除。 也許未清除的緩存是您在dialog.dismiss()之前在對話框中看到的最后一個緩存。
使用此memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE)
Picasso.with(activity).load(file).resize(100,100).centerCrop().memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE).into(contactImage, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
}
});
Picasso.with(this.getContext()).load(gamePlayer.getPlayerProfileUrl()).skipMemoryCache().into(iv);
這也有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.