【问题标题】:android picasso clear cacheandroid毕加索清除缓存
【发布时间】:2014-04-10 04:57:15
【问题描述】:

我正在使用毕加索显示一个人的肖像,当肖像改变时,我想清除这个用户的缓存(或所有用户的肖像缓存),这是我的代码,它不起作用,任何人都可以帮助我?

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);

【问题讨论】:

标签: android caching picasso


【解决方案1】:

在最近的 Picasso 版本中,有一种新的无效方法,没有任何变通方法,所以我认为前面提到的自定义 PicassoTools 类现在已经过时了

Picasso.with(getActivity()).invalidate(file);

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,没有一个解决方案对我来说是可以接受的......所以我至少提出了一个反思的解决方案。我不使用裁剪、调整大小等,所以我的清除功能只有在你只使用 load(url) 和调整大小、裁剪、转换时才有效……但我自定义了完整的 getKey 函数,所以这至少应该是对扩展清除功能有很大帮助。或者只是将getKeyfunction 公开并将clear(uri) 函数更改为clear(key)...

    我只是从源代码中复制了关键功能并采用了它。

    只需从我的工具类中获取 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);
        }
    }
    }
    

    PS:灵感来自Invalidate cache in Picasso

    【讨论】:

      猜你喜欢
      • 2016-08-10
      • 2015-02-14
      • 2019-05-26
      • 2015-05-07
      • 2016-01-20
      • 2016-12-31
      • 2016-03-17
      • 2016-04-28
      • 2015-08-24
      相关资源
      最近更新 更多