Picasso在第一次通话时不加载图像

问题描述 投票:2回答:2

我遇到了一个非常有趣的问题。我正在使用波纹管代码使用picasso加载位图:

           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种不同的方法中将不同的消息打印到日志中。

关于发生了什么或者我错过了什么的想法?

先感谢您。

java android picasso
2个回答
1
投票

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

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

您也可以尝试使用Glide https://github.com/bumptech/glide


1
投票

问题:问题是Picasso对目标类有一个弱引用,它得到了GARBAGE COLLECTED。

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

© www.soinside.com 2019 - 2024. All rights reserved.