如何在 Picasso 中使用磁盘缓存? [英] How do I use disk caching in Picasso?

查看:24
本文介绍了如何在 Picasso 中使用磁盘缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Picasso 在我的 android 应用中显示图像:

I am using Picasso to display image in my android app:

/**
* load image.This is within a activity so this context is activity
*/
public void loadImage (){
    Picasso picasso = Picasso.with(this); 
    picasso.setDebugging(true);
    picasso.load(quiz.getImageUrl()).into(quizImage);
}

我启用了调试,它总是显示红色和绿色.但从不显示黄色

I have enable debugging and it always shows either red and green .But never shows yellow

现在,如果我下次加载相同的图像而互联网不可用,则不会加载图像.

Now if i load same image next time and internet is not available the image is not loaded.

问题:

  1. 它没有本地磁盘缓存吗?
  2. 如何启用磁盘缓存,因为我将多次使用相同的图像.
  3. 我需要为 android manifest 文件添加一些磁盘权限吗?

推荐答案

这就是我所做的.效果很好.

This is what I did. Works well.

首先在app模块的gradle构建文件中添加OkHttp:

First add the OkHttp to the gradle build file of the app module:

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp3:okhttp:3.10.0'
compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'

然后创建一个扩展Application

import android.app.Application;

import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;

public class Global extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        Picasso.Builder builder = new Picasso.Builder(this);
        builder.downloader(new OkHttp3Downloader(this,Integer.MAX_VALUE));
        Picasso built = builder.build();
        built.setIndicatorsEnabled(true);
        built.setLoggingEnabled(true);
        Picasso.setSingletonInstance(built);

    }
}

将其添加到 Manifest 文件中,如下所示:

add it to the Manifest file as follows :

<application
        android:name=".Global"
        .. >

</application>

现在像往常一样使用毕加索.没有变化.

Now use Picasso as you normally would. No changes.

如果您只想使用缓存图像.像这样调用图书馆.我注意到,如果我们不添加 networkPolicy,图像不会在完全脱机启动时显示即使它们被缓存.下面的代码解决了这个问题.

if you want to use cached images only. Call the library like this. I've noticed that if we don't add the networkPolicy, images won't show up in an fully offline start even if they are cached. The code below solves the problem.

Picasso.with(this)
            .load(url)
            .networkPolicy(NetworkPolicy.OFFLINE)
            .into(imageView);

编辑#2

上面代码的问题是,如果你清除缓存,Picasso会一直在缓存中离线查找,失败,下面的代码示例查看本地缓存,如果离线找不到,则上线并补充缓存.

the problem with the above code is that if you clear cache, Picasso will keep looking for it offline in cache and fail, the following code example looks at the local cache, if not found offline, it goes online and replenishes the cache.

Picasso.with(getActivity())
.load(imageUrl)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(imageView, new Callback() {
    @Override
    public void onSuccess() {

    }

    @Override
    public void onError() {
        //Try again online if cache failed
        Picasso.with(getActivity())
                .load(posts.get(position).getImageUrl())
                .error(R.drawable.header)
                .into(imageView, new Callback() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError() {
                Log.v("Picasso","Could not fetch image");
            }
        });
    }
});

这篇关于如何在 Picasso 中使用磁盘缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆