3

当我在他们的Github 页面上使用 Picasso 的示例项目时,图像被缓存了。意味着一旦加载它们,即使我关闭互联网连接,它们也会出现。

但是当我使用相同的方法从不同项目中的相同 URL下载图像时,图像不会被缓存。我也在使用 Android 4.2.2(磁盘缓存需要 ICS+)。那么这里可能是什么问题呢?

这是他们使用的简单代码,我正在使用

Picasso.with(context) //
        .load(url) //
        .placeholder(R.drawable.placeholder) //
        .error(R.drawable.error) //
        .fit() //
        .into(view);
4

2 回答 2

3

如果您仅使用 Picasso 库,则不会发生缓存。使用 okhttp 进行缓存。例如:如果您使用的是 picasso-2.1.1.jar,则使用 okhttp-1.2.1-jar-with-dependencies.jar 进行缓存。

否则尝试 Glide 库,它类似于 picasso 的实现。在从缓存中加载图像时效果很好.... 查看 Glide Glide Github Example 2

于 2014-09-10T04:47:00.860 回答
1

在 build.gradle

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// You must install or update the Google Repository through the SDK manager to use this dependency.
// You must install or update the Support Repository through the SDK manager to use this dependency.
// You must install or update the Support Repository through the SDK manager to use this dependency.
oldCompile 'com.android.support:appcompat-v7:21.0.2'
compile 'com.google.android.gms:play-services:4.2.42'
compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp:2.2.0' //updated this
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0' //updated this
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.picasso:picasso:2.5.0' //updated this
compile files('libs/robotium-solo-5.2.1.jar')
}

它既没有为我缓存图像,但是当我将 OkHttp、OkHttp-urlconnection、Picasso 更新到最新版本时,它就起作用了。尝试http://gradleplease.appspot.com以获取最新版本。

于 2015-02-10T15:48:37.703 回答