2017-03-16 90 views
1

我正在使用picasso从网址加载图像。由于我需要位图进行进一步处理,因此我使用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); 
          } 
+0

Picasso.with(本) .load( “图像URL HERE”) .into(ImageView的); –

+0

你的设置在imageview代码中? –

+0

您应该尝试将图像加载到毕加索以保存图像。 –

回答

3

它是一个已知的问题,如毕加索只保留一个星期参考:

解决这一问题将是目标为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发布!

0

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

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

其中, carImg是XML的ImageView的ID, carImageUrl是资源

0

试试这个功能,我使用: 并使用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); 
     } 
    }