Home » Android » Images are not loading in android 5.0 using picasso library

Images are not loading in android 5.0 using picasso library

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am working on demo application in which I am using Picasso library v2.5.2. It is working fine on all android operating system version, but not in lollipop.

Image whose size is 130KB which is not loading for me. Images whose size is less are loading correctly.

Here is my code for downloading bitmap and set on imageview.

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

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

I’m not sure what extra stuff I have to do with this so that I will work on lollipop also or this is bug in lib ?

How to&Answers:

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.

Answer:

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.

Answer:

        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.