【问题标题】:Android Picasso cache dataAndroid Picasso 缓存数据
【发布时间】:2018-08-27 09:27:46
【问题描述】:

在我的应用程序中,我想从 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;
        }

【问题讨论】:

    标签: android caching picasso


    【解决方案1】:

    你好,你可以试试这个:

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

    【讨论】:

      【解决方案2】:

      我对毕加索了解不多。 我有一个建议,你必须使用 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.
              }
          });
      

      【讨论】:

        【解决方案3】:

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

        解释清楚的答案在这里https://stackoverflow.com/a/57114949/1508631

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-01
          • 2018-08-22
          • 2023-03-13
          相关资源
          最近更新 更多