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

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

问题描述

我使用的毕加索在我的Andr​​oid应用程序显示的图像:

  / **
*负荷image.This是一个活动中所以这方面的活动
* /
公共无效的LoadImage(){
    毕加索毕加索= Picasso.with(本);
    picasso.setDebugging(真正的);
    picasso.load(quiz.getImageUrl())到(quizImage)。
}
 

我已经启用调试和它总是显示是红色和绿色的。但绝不会显示黄色

现在,如果我加载相同的图像下一次和互联网无法使用的图像不会加载。

问题:

  1. 在这岂不是有本地的磁盘缓存?
  2. 如何启用磁盘缓存,因为我将使用相同​​的图像多次。
  3. 请我需要添加一些磁盘权限的Andr​​oid清单文件?
解决方案

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

首先OkHttp添加到应用程序模块的摇篮构建文件

 编译com.squareup.picasso:毕加索:2.5.2
编译com.squareup.okhttp:okhttp:2.4.0
 

然后,让一类推广应用

 进口android.app.Application;

进口com.squareup.picasso.OkHttpDownloader;
进口com.squareup.picasso.Picasso;

公共类全球扩展应用{
    @覆盖
    公共无效的onCreate(){
        super.onCreate();

        Picasso.Builder建设者=新Picasso.Builder(本);
        builder.downloader(新OkHttpDownloader(这一点,是Integer.MAX_VALUE));
        毕加索内置= builder.build();
        built.setIndicatorsEnabled(真正的);
        built.setLoggingEnabled(真正的);
        Picasso.setSingletonInstance(内置);

    }
}
 

将其添加到清单文件如下:

 <应用
        机器人:环球NAME =
        ..>

< /用途>
 

现在使用毕加索像平时那样。没有变更。

编辑:

如果你只想使用缓存图像

。这样调用的库。我注意到,如果我们不添加networkPolicy,图片不会出现在一个完全离线开始的即使他们被缓存。下面的code解决了这个问题。

  Picasso.with(本)
            .load(URL)
            .networkPolicy(NetworkPolicy.OFFLINE)
            。走进(ImageView的);
 

编辑#2

与上面的code中的问题是,如果你清除缓存,毕加索将保持脱机寻找它的缓存和失败,以下code例如着眼于本地缓存,如果没有线下找到,上线和补充缓存。

  Picasso.with(getActivity())
.load(IMAGEURL)
.networkPolicy(NetworkPolicy.OFFLINE)
。走进(ImageView的,新的回调(){
    @覆盖
    公共无效的onSuccess(){

    }

    @覆盖
    公共无效onerror的(){
        //再试一次网上,如果缓存失败
        Picasso.with(getActivity())
                .load(posts.get(位置).getImageUrl())
                .error(R.drawable.header)
                。走进(ImageView的,新的回调(){
            @覆盖
            公共无效的onSuccess(){

            }

            @覆盖
            公共无效onerror的(){
                Log.v(毕加索,无法获取图像);
            }
        });
    }
});
 

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.

Questions:

  1. Does it not have local disk cache?
  2. How do i enable disk caching as i will be using same image multiple times.
  3. Do i need to add some disk permission to android manifest file?

解决方案

This is what i did. Works well

first add the OkHttp to the gradle build file of the app module

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp:okhttp:2.4.0'

Then make a class extending Application

import android.app.Application;

import com.squareup.picasso.OkHttpDownloader;
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 OkHttpDownloader(this,Integer.MAX_VALUE));
        Picasso built = builder.build();
        built.setIndicatorsEnabled(true);
        built.setLoggingEnabled(true);
        Picasso.setSingletonInstance(built);

    }
}

add it to the Manifest file as follows :

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

</application>

Now use Picasso as you normally would. No changes.

EDIT:

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

EDIT #2

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");
            }
        });
    }
});

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

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