毕加索似乎没有使用我给它的memoryCache

时间:2015-04-27 18:08:18

标签: android picasso

在我正在研究的项目中(名为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实例。但是,之后没有-set-或-set-或任何其他方法在BoxsetterImageCache实例上调用(我可以通过没有大量日志触发来判断,并且没有创建文件)。

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

1 个答案:

答案 0 :(得分:0)

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

我用磁盘缓存创建了自己的Picasso实例,但随后调用了with(context)方法。 with(context)总是返回Picasso单例而不是调用它的实例,所以我立即将控件从我自己的实例中移开。

两个选项:

  1. 删除with(context)

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

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

    谢谢!