安卓Picasso清除缓存

10

我正在使用Picasso来展示一个人的肖像。当这个肖像被更改后,我想要清除这个用户的缓存(或所有用户的肖像缓存)。这是我的代码,但它没有起作用。有谁能帮忙解决一下吗?

LruCache lruCache = new LruCache(context);
lruCache.clear();
Picasso picasso = new Picasso.Builder(context).memoryCache(lruCache).build();
picasso.load(portraitUrl).resize(50, 50).centerCrop().error(R.drawable.user_portrait).into(portaitView);

1
这是你的答案:https://dev59.com/rWEh5IYBdhLWcg3wyGDy - Renan Franca
2个回答

8
在最近版本的Picasso中,有一种新的invalidate方法,无需任何解决方法,因此我认为之前提到的自定义PicassoTools类现在已经过时。
Picasso.with(getActivity()).invalidate(file);

2
我曾经遇到同样的问题,但是没有一个解决方案能够令我满意。所以我使用了反射机制来解决这个问题。我没有使用裁剪、调整大小等操作,因此我的清除函数只有在你使用load(url)且进行了调整大小、裁剪或其他变换操作时才能正常工作。但是我自定义了完整的getKey函数,这至少可以帮助你扩展清除函数。或者你也可以将getKey函数设为公共函数,并将clear(uri)函数改为clear(key)

我只是从源代码中复制了key函数并加以改编。

只需从我的tools类中获取picasso实例,一切都应该能够正常工作(不需要像我一样提供上下文提供程序,例如,你可以添加一个init函数来初始化PicassoTools类,例如使用应用程序上下文)。

只需使用以下工具类:

public class PicassoTools
{
private static Picasso picasso = null;
private static CustomLruCache lruCache = null;

private static CustomLruCache getCache()
{
    if (lruCache == null)
        lruCache = new CustomLruCache(MainApp.getAppContext());
    return lruCache;
}

public static Picasso getPicasso()
{
    if (picasso == null)
        picasso = new Picasso.Builder(MainApp.getAppContext()).memoryCache(getCache()).build();
    return picasso;
}

public static void clear(Uri uri)
{
    getCache().remove(getKey(uri));
}

public static void clearCache()
{
    getCache().clear();
    // Picasso.with(MainApp.getAppContext()).cache.clear();
}

public static void clearCache(Context c)
{
    getCache().clear();
    // Picasso.with(c).cache.clear();
}

private static final int KEY_PADDING = 50; // Determined by exact science.

private static String getKey(Uri uri)
{
    return getKey(uri, null, 0, 0, false, false, null);
}

private static String getKey(Uri uri, Integer resourceId, int targetWidth, int targetHeight, boolean centerCrop, boolean centerInside, List<Transformation> transformations)
{
    StringBuilder builder = new StringBuilder();
    if (uri != null)
    {
        String path = uri.toString();
        builder.ensureCapacity(path.length() + KEY_PADDING);
        builder.append(path);
    }
    else
    {
        builder.ensureCapacity(KEY_PADDING);
        builder.append(resourceId);
    }
    builder.append('\n');

    if (targetWidth != 0)
    {
        builder.append("resize:").append(targetWidth).append('x').append(targetHeight);
        builder.append('\n');
    }
    if (centerCrop)
    {
        builder.append("centerCrop\n");
    }
    else if (centerInside)
    {
        builder.append("centerInside\n");
    }

    if (transformations != null)
    {
        // noinspection ForLoopReplaceableByForEach
        for (int i = 0, count = transformations.size(); i < count; i++)
        {
            builder.append(transformations.get(i).key());
            builder.append('\n');
        }
    }

    return builder.toString();
}
}

扩展缓存类:

public class CustomLruCache extends LruCache
{
public CustomLruCache(Context context)
{
    super(context);
}

public CustomLruCache(int value)
{
    super(value);
}

@Override
public Bitmap get(String key)
{
    L.d(this, key);
    return super.get(key);
}

public void remove(String key)
{
    try
    {
        Bitmap value = map.remove(key);

        Field fieldSize = LruCache.class.getDeclaredField("size");
        fieldSize.setAccessible(true);
        Integer size = (Integer) fieldSize.get(this);
        size -= Utils.getBitmapBytes(value);
        fieldSize.set(this, size);

        Field fieldEvictionCount = LruCache.class.getDeclaredField("evictionCount");
        fieldEvictionCount.setAccessible(true);
        Integer evictionCount = (Integer) fieldEvictionCount.get(this);
        evictionCount++;
        fieldEvictionCount.set(this, evictionCount);

    }
    catch (IllegalArgumentException e)
    {
        L.e(this, e);
    }
    catch (IllegalAccessException e)
    {
        L.e(this, e);
    }
    catch (NoSuchFieldException e)
    {
        L.e(this, e);
    }
}
}

提示:灵感来自Picasso中的缓存失效


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接