Images are not loading in android 5.0 using picasso library

北战南征 提交于 2019-12-04 03:12:23

It's a known problem. The problem is that Picasso keeps a weak reference for the Target. To get it working you need to make it strong, by storing a Target as a tag of view, for example.

target = new Target() {
    @Override
    public void onPrepareLoad(Drawable drawable) {}

    @Override
    public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
        if(bitmap != null) {
            imageView.setImageBitmap(bitmap);
        }
    }

    @Override
    public void onBitmapFailed(Drawable drawable) {}
    };

imageView.setTag(target);
Picasso.with(this).load(URL).into((Target) imageView.getTag());

EDIT:

I suggest you to use Glide, it's very similar to Picasso, and also recommended by Google. And as you can see in the end of this thread, the original developer solves this BitmapFactory problem by using extra buffer.

Why would you use a Target if you only need to load the image into the ImageView? Just use this:

    Picasso.with(this).load(URL).into(imageView, new Callback()
    {
        @Override
        public void onSuccess()
        {
            //Dimiss progress dialog here
        }

        @Override
        public void onError()
        {
            //And here
        }
    });

For documentation look here.

        Picasso.with(this).load("http://webneel.com/wallpaper/sites/default/files/images/04-2013/island-beach-scenery-wallpaper.jpg").placeholder(R.mipmap.ic_launcher).fit().into(imageView, new Callback()      { 
    @Override public void onSuccess() 
    {
     } 
   @Override public void onError() 
   {
    }
     });

fit() will help you to load image.And use android:adjustViewBounds="true" in your ImageView in xml.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!