2017-03-16 10 views
1

私は、URLから画像を読み込むためにピカソを使用しています。後で処理するためにビットマップが必要なので、ビットマップを保存するためにTarget()クラスを使用しています。しかしピカソは最初の実行で画像を読み込んでいません。しかし、私は別のアクティビティに行き、ピカソの活動に取りかかったときに読み込まれます。なぜそれが起こっているのですか?すべての修正?私のコードは以下の通りです、Picasa画像は最初の実行時に読み込まれません

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") .into(ImageViewの); –

+0

あなたのイメージビューコードはどこですか? –

+0

画像をPicassoに読み込んで画像を保存しないようにしてください。 –

答えて

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

適切な説明がSO投稿thisに与えられています!

0

イメージの読み込みに使用できます。

Picasso.with(getActivity())。load(carImageUrl).into(carImg); をそしてimg.setTag(/*some other object than path of file or errId. But don't forget to add it before using this function*/)を使用します。

、 carImgはXMLでImageViewののIDで、 carImageUrlは、リソース

0

私が使用してこの機能を試してみてくださいです。同じ方法で使用したくない場合は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); 
     } 
    } 
関連する問題