Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images are not loading in android 5.0 using picasso library

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 ?

like image 374
N Sharma Avatar asked May 28 '15 10:05

N Sharma


People also ask

What is the latest version of Picasso Android?

The latest version is 2.71828. Show activity on this post. Version 2.8 is the latest as of today's post Oct 04 2020 which was released this on Aug 10 2020.


1 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.

like image 141
romtsn Avatar answered Oct 20 '22 06:10

romtsn