毕加索磁盘缓存无法正常工作

时间:2017-08-28 11:41:14

标签: android caching dependency-injection picasso

我使用毕加索来下载和缓存图片,但图片没有缓存但是下载成功,我没有完成问题,我也用匕首来体现普拉索对象,但我不知道认为这是问题

这是我在适配器上的代码,因为我将picasso传递给适配器对象

    picasso.load(currentClient.getLogoUrl())
            .placeholder(R.mipmap.ic_launcher)
            .into(holder.clientImage);

我的代码

@Module(includes = {ContextModule.class, NetworkModule.class})
public class PicassoModule {

    @Provides
    @InmaaApplicationScope
    public Picasso picasso(@ApplicationContext Context context, OkHttp3Downloader okHttp3Downloader) {
        return new Picasso.Builder(context)
                .downloader(okHttp3Downloader)
                .build();
    }

    @Provides
    @InmaaApplicationScope
    public OkHttp3Downloader okHttp3Downloader(OkHttpClient okHttpClient) {
        return new OkHttp3Downloader(okHttpClient);
    }
}

另一个模块

@Module(includes = ContextModule.class)
public class NetworkModule {

    @Provides
    @InmaaApplicationScope
    public HttpLoggingInterceptor loggingInterceptor() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> Timber.i(message));
        interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        return interceptor;
    }

    @Provides
    @InmaaApplicationScope
    public Cache cache(File cacheFile) {
        return new Cache(cacheFile, 512 * 1024 * 1024); //512 MB for cache
    }

    @Provides
    @InmaaApplicationScope
    public File cacheFile(@ApplicationContext Context context) {
        return new File(context.getCacheDir(), "okhttp_cache");
    }

    @Provides
    @InmaaApplicationScope
    public OkHttpClient okHttpClient(HttpLoggingInterceptor loggingInterceptor, Cache cache) {
        return new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .cache(cache)
                .build();
    }

}

在活动代码

  @Inject
    Picasso picasso;

        clientsAdapter = new ClientsAdapter(picasso);

缓存文件夹的状态

enter image description here

1 个答案:

答案 0 :(得分:0)

我想你忘了实际设置Picasso实例:

Picasso.setSingletonInstance(/* insert your instance here*/));

必须在用Picasso加载任何图像之前完成。

由于您使用自己的Picasso实例,我猜图像没有正确的缓存标头。请参阅以下评论。