【发布时间】: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