【问题标题】:Picasso image is not loading on first run首次运行时未加载毕加索图像
【发布时间】:2017-03-16 05:34:25
【问题描述】:

我正在使用 picasso 从 url 加载图像。由于我需要位图进行进一步处理,因此我使用 Target() 类来保存位图。但是毕加索在第一次运行时没有加载图像。但它在我去另一个活动并回到毕加索实施的活动时加载。为什么会这样?有什么修复吗?我的代码如下,

 Picasso.with(getActivity()).load(card.getExtras().getImageUrl()).into(new Target() {
                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                            SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
                            Date now = new Date();
                            filename ="certificate_"+ formatter.format(now) + ".png";

                            File path=null;
                            if (getActivity().getExternalCacheDir()==null) {

                               path=getActivity().getCacheDir();
                            }
                            if(getActivity().getExternalCacheDir()!=null){
                                path=getActivity().getExternalCacheDir();
                            }
                           File  image=new  File(path+filename);
                            FileOutputStream fileOutPutStream = null;
                            try {
                                fileOutPutStream = new FileOutputStream(image);
                                bitmap.compress(Bitmap.CompressFormat.PNG, 80, fileOutPutStream);

                                fileOutPutStream.flush();
                                fileOutPutStream.close();
                                Log.d("---REACHED","FILE SAVED--------------");
                            } catch (Exception e) {

                                Crashlytics.logException(e);
                            }

【问题讨论】:

  • Picasso.with(this) .load("您的图片网址在这里") .into(imageView);
  • 你在哪里设置成imageview代码?
  • 您应该尝试将图像加载到毕加索与保存图像分开。
  • 我会建议你使用 Glide

标签: android picasso


【解决方案1】:

这是一个已知问题,因为毕加索只保留一周的参考:

解决此问题的方法是将目标设置为 tag 到您希望设置的视图组件。

所以您的代码将如下所示:

Target target = new Target() {
                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                           .....
// set the tag to the view
holder.imageView.setTag(target);

//set the target to picasso
Picasso.with(getActivity()).load(card.getExtras().getImageUrl()).into(target);

this SO 帖子中给出了相同的正确解释!

【讨论】:

    【解决方案2】:

    您可以使用它来加载图像。

    Picasso.with(getActivity()).load(carImageUrl).into(carImg);

    在哪里, carImg 是 XML 中 Imageview 的 id, carImageUrl 是一种资源

    【讨论】:

      【解决方案3】:

      试试我使用的这个功能: 并使用img.setTag(/*some other object than path of file or errId. But don't forget to add it before using this function*/)。如果您不想以相同的方式使用它,请在检查getTag() 时删除if 条件。

      public static void setImage(final Context context, final ImageView img, @DrawableRes final int defId,
                                      @DrawableRes final int errId, final File file, Picasso.Priority priority) {
              if (null != img.getTag()) {
                  if (null == img.getDrawable() || !(img.getTag() instanceof String && (img.getTag().equals(file.getAbsolutePath())))) {
                      try {
                          if (file.exists()) {
                              Picasso.with(context.getApplicationContext())
                                      .load(file)
                                      .priority(priority)
                                      .placeholder(defId)
                                      .error(errId)
                                      .fit()
                                      .centerInside()
                                      .tag(context)
                                      .noFade()
                                      .into(img, new Callback() {
                                          @Override
                                          public void onSuccess() {
                                              img.setTag(file.getAbsolutePath());
                                          }
      
                                          @Override
                                          public void onError() {
                                              img.setTag(errId);
                                          }
                                      });
                          } else {
                              img.setImageResource(defId);
                              img.setTag(defId);
                          }
      
                      } catch (Exception e) {
                          img.setImageResource(defId);
                          img.setTag(defId);
                      }
                  }
              } else {
                  img.setImageResource(defId);
                  img.setTag(defId);
              }
          }
      

      【讨论】:

        【解决方案4】:

        ViewGroup (RelativeLayout, LinearLayout, FrameLayout 等加载毕加索图像

        在我的情况下,以下方式有效。

        需要HandlerThreadHandler 来加载图片。

        kotlin 中的以下示例。您可以根据需要在 Java 中进行转换。

        val handlerThread = HandlerThread("ImageLoader")
        handlerThread.start()
        
        val handler = Handler(handlerThread.looper)
        handler.post({
            var bitmap: Bitmap? = null
            try {
                bitmap = Picasso.with(this).load(iamgeUrl).get()
            } catch (e: IOException) {
                e.printStackTrace()
            } finally {
                if (bitmap != null) {
                    runOnUiThread({
                        imageView.background = BitmapDrawable(resources, bitmap)
                    })
                }
            }
        })
        

        希望这会对你有所帮助。

        【讨论】:

          【解决方案5】:

          您可以尝试将占位符属性添加到毕加索:

          Picasso.with(this).load(imageData)
                 .placeholder(R.drawable.placeholder)
                 .resize(200,200)
                 .into(mImageView)
          

          希望对你有所帮助!

          【讨论】:

            【解决方案6】:

            我知道这是一个老问题,但对于那些想知道的人来说。问题是由于目标变量的弱引用,所以只需全局定义目标,如果您更喜欢作为类变量并解决问题

            【讨论】:

              猜你喜欢
              • 2016-07-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-08-29
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多