2017-05-29 80 views
1

我遇到了一个非常复杂的问题。我现在用的波纹管代码中使用毕加索加载位图:毕加索在第一次来电时不会加载图像

  final Target target = new Target() { 
       @Override 
       public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
        // loaded bitmap is here (bitmap) 
        Log.i(TAG, "bitmapLoaded"); 
        imageView.setImageBitmap(bitmap); 
       } 

       @Override 
       public void onBitmapFailed(Drawable errorDrawable) { 
        Log.i(TAG, "bitmapFailed"); 
       } 

       @Override 
       public void onPrepareLoad(Drawable placeHolderDrawable) { 

       } 
      }; 

      imageView.setTag(target); 

      Picasso.with(this) 
        .load(photoUrl) 
        .into(target); 

我知道了很多问题已经被问毕加索不加载图像由于弱引用,但我不认为是这样的话,因为我有遵循许多主题中提出的解决方案来引用上面的目标。

在我的程序中,我在3个不同的类和3个不同的时刻使用了相同的代码。我注意到的是,无论什么时候我第一次调用这个方法都不起作用,但是在下一次工作时,3个调用中的哪一个并不重要。我可以这样说,因为我从这3种不同的方法向日志打印了不同的消息。

有关正在发生的事情或我错过了什么的任何想法?

预先感谢您。

回答

0

尝试使用异步方法实现此目的。你

Picasso.with(context).load(URL).into(profile, new Callback() { 
       @Override 
       public void onSuccess() { 
        new Handler().postDelayed(new Runnable() { 
         @Override 
         public void run() {//Use your "bitmap" here 

          Bitmap innerBitmap = ((BitmapDrawable) profile.getDrawable()).getBitmap(); 
        } 
       }, 100); 
      } 

也可以尝试使用滑翔https://github.com/bumptech/glide

+0

我尝试使用你的例子,但位图没有加载到imageView中,尽管调用了run()方法。 –

+0

这是同样的问题,我第一次称这种方法它不起作用,但下一次它会。这很奇怪。 –

0

问题:问题是毕加索持有弱引用对象类,它有垃圾回收

解决方案:将其转换为类字段,而不是将其用作本地参考。