毕加索好像没有用我给它的memoryCache

Picasso doesn't seem to use the memoryCache I give it

在我正在处理的项目(名为 Boxsetter)中,我试图让 Picasso 使用我创建的磁盘缓存(BoxsetterImageCache,实现 Cache 接口):

new Picasso.Builder(activity)
   .memoryCache(new BoxsetterImageCache(activity))
   .build()
   .with(activity)
   .load(be.getImg())
   .placeholder(R.drawable.boxsetter2)
   .into(imageView);

通过日志记录,我可以看到创建了 BoxsetterImageCache 实例。但是,之后没有在 BoxsetterImageCache 实例上调用 -get- 或 -set- 或任何其他方法(我可以通过没有大量日志触发和没有创建文件来判断)。

Picasso 的其余部分工作正常:占位符被放置在 ImageView 中,然后愉快地替换为通过 http 访问的图像。但是,它似乎并没有在缓存上调用方法。

关于为什么会这样有什么想法吗?这个实现有问题吗?

干杯

尼克

BoxsetterImageCache:

package com.boxsetter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.squareup.picasso.Cache;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * Created by nic.ford on 27/04/15.
 */
public class BoxsetterImageCache implements Cache {

    private String filesDir;

    public BoxsetterImageCache(BoxsetterActivity ba) {
        Log.d("BSBIC", "BIC created"); // THIS LOG IS SEEN
        this.filesDir = ba.getExternalFilesDir(null).getAbsolutePath();
    }

    @Override
    public Bitmap get(String key) {
        File file = new File(filesDir + "/bmps/" + key);

        // THIS LOG IS NEVER SEEN
        Log.d("BSBIC", "get() File on path: " + file.getAbsolutePath());

        if (file.exists()) {
            try {
                return BitmapFactory.decodeStream(new FileInputStream(file));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public void set(String key, Bitmap bitmap) {
        File file = new File(filesDir + "/bmps/" + key);

        // THIS LOG IS NEVER SEEN
        Log.d("BSBIC", "set() File on path: " + file.getAbsolutePath());

        try {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        File dir = new File(filesDir + "/bmps/");

        // THIS LOG IS NEVER SEEN
        Log.d("BSBIC", "size() File on path: " + dir.getAbsolutePath());

        int len = 0;

        for (File file : dir.listFiles()) {
            len += file.length();
        }

        return len;
    }

    @Override
    public int maxSize() {
        File file = new File(filesDir + "/bmps/");

        // THIS LOG IS NEVER SEEN
        Log.d("BSBIC", "maxSize() File on path: " + file.getAbsolutePath());

        return (int)file.getFreeSpace();
    }

    @Override
    public void clear() {
        File dir = new File(filesDir + "/bmps/");

        // THIS LOG IS NEVER SEEN
        Log.d("BSBIC", "clear() File on path: " + dir.getAbsolutePath());

        for (File file : dir.listFiles()) {
            if (file.exists()) file.delete();
        }
    }

    @Override
    public void clearKeyUri(String keyPrefix) {
        File dir = new File(filesDir + "/bmps/");

        // THIS LOG IS NEVER SEEN
        Log.d("BSBIC", "clearKeyUri() File on path: " + dir.getAbsolutePath());

        for (File file : dir.listFiles()) {
            if (file.getName().startsWith(keyPrefix)) file.delete();
        }
    }
}

所以,Nightly Nexus 找到了答案(谢谢)。

我正在使用我的磁盘缓存创建我自己的 Picasso 实例,然后在其上调用 with(context) 方法。 with(context) 总是 returns Picasso 单例而不是调用它的实例,所以我立即将控制权从我自己的实例中移走。

两个选项:

  1. 删除with(context),或

  2. 使用Picasso.setSingletonInstance(mypicasso)用我的实例替换现有的单例

我选择了前者,现在效果很好。所以现在我可以稍微重构一下,去掉主线程磁盘 I/O.

谢谢!