【问题标题】:Picasso disk cache not working毕加索磁盘缓存不起作用
【发布时间】:2018-02-05 16:08:57
【问题描述】:

我使用 picasso 下载和缓存图像,但图像没有缓存但下载成功,我没有找到问题,我也使用 dagger 来体面地注入 picasso 对象,但我不认为这是问题

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

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

还有我的di代码

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

缓存文件夹的状态

【问题讨论】:

    标签: android caching dependency-injection picasso


    【解决方案1】:

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

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

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

    由于您使用的是自己的 Picasso 实例,我猜图像没有适当的缓存标头。请参阅下面的 cmets。

    【讨论】:

    • 我不使用 picasso Singleton 我使用 di 所以我将它作为参数传递给适配器我将但活动代码以获得更多解释
    • Picasso 使用两种缓存机制:由 Picasso 自己管理的内存缓存和由 Http-Client 管理的磁盘缓存。我猜你想用第二个。 (Ok)Http Client 缓存依赖于图片服务器发送的缓存头。也许那些是无效的?
    • 图片来自 Firebase 存储
    • 您可以向 OkHttpClient 实例添加一个 Loggin-Interceptor 以查看哪些缓存标头处于活动状态。以下是如何设置它们的文档链接:firebase.google.com/docs/hosting/full-config#headers
    • 我已经添加了拦截器但是如何知道活动缓存头]
    猜你喜欢
    • 2016-06-18
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 2013-04-27
    • 2014-02-13
    • 2014-10-16
    • 2015-06-23
    相关资源
    最近更新 更多