繁体   English   中英

Android毕加索缓存数据

[英]Android Picasso cache data

在我的应用程序中,我想从 url 下载图像并在 recyclerView 中显示它们。 基本上一切正常 - 当我下载图像、关闭 wifi 和移动数据时 - 正在显示缓存的图像。 尝试了几次 - 它完美无缺。 但是...例如 3-4 小时后,我再次尝试以离线模式启动应用程序,但未显示图像。 知道我做错了什么吗? 这是我在基本活动 (onCreate) 中的代码:

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

然后我像这样下载并缓存图像:

public void onResponse(Call<CallbackListPost> call, Response<CallbackListPost> response) {
                CallbackListPost resp = response.body();
                if (resp != null && resp.status.equals("ok")) {
                    post_total = resp.count_total;
                    displayApiResult(resp.posts);
                    controller = new RealmController(getActivity().getApplication());
                    controller.savePost(resp.posts);
                    for(Post post : resp.posts) {
                        for (final Attachment att : post.attachments) {
                            Picasso.with(getActivity())
                                    .load(att.url)
                                    .fetch(new com.squareup.picasso.Callback() {
                                        @Override
                                        public void onSuccess() {
                                            Log.e(TAG, "onSuccess: " + att.url );
                                        }

                                        @Override
                                        public void onError() {

                                        }
                                    });
                        }
                    }

然后在第二个活动中我显示这样的图像:

public View getView(int position, View convertView, ViewGroup parent) {


            ImageView iv;

            if(convertView == null) {
                iv = new ImageView(mContext);
            } else {
                iv = (ImageView) convertView;
            }

            Picasso.with(mContext)
                    .load(att.get(position).url)
                    .noFade()
                    .resize(150,150)
                    .centerCrop()
                    .into(iv);
                return iv;
        }

您好,您可以试试这个:

Picasso.with(mContext)
                .load(att.get(position).url)
                .noFade()
                .resize(150,150)
                .centerCrop()
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .networkPolicy(NetworkPolicy.NO_CACHE)
                .into(iv);

我对毕加索了解不多。 我有一个建议,你必须使用 Glide,一个图像缓存工具。 它的图书馆很小而且速度很快。

Glide 生成的 API 的简单用例看起来像这样:

// 对于一个简单的视图:

@Override public void onCreate(Bundle savedInstanceState) {
  ...
  ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

  GlideApp.with(this).load("goo.gl/gEgYUd").into(imageView);
}

// For a simple image list:
@Override public View getView(int position, View recycled, ViewGroup container) {
  final ImageView myImageView;
  if (recycled == null) {
    myImageView = (ImageView) inflater.inflate(R.layout.my_image_view, container, false);
  } else {
    myImageView = (ImageView) recycled;
  }

  String url = myUrls.get(position);

  GlideApp
    .with(myFragment)
    .load(url)
    .centerCrop()
    .placeholder(R.drawable.loading_spinner)
    .into(myImageView);

  return myImageView;
}

它有自动缓存配置,这意味着我们不需要担心图像缓存。它在第一次使用后完美加载。

在 Glide V4 中

Glide.with(context)
    .load(“/user/profile/photo/path”)
    .asBitmap()
    .toBytes()
    .centerCrop()
    .into(new SimpleTarget<byte[]>(250, 250) {
        @Override
        public void onResourceReady(byte[] data, GlideAnimation anim) {
            // Post your bytes to a background thread and upload them here.
        }
    });

如果您参考最新版本的 Picasso (2.71828) 的文档并弄清楚 Picasso 内部是如何处理它的,您就会明白为什么您的图像没有从磁盘/缓存中加载。

解释得很好的答案在这里https://stackoverflow.com/a/57114949/1508631

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM