Images are not loading in android 5.0 using picasso library

匆匆过客 提交于 2019-12-12 08:33:39

问题


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 ?


回答1:


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.




回答2:


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.




回答3:


        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.



来源:https://stackoverflow.com/questions/30503625/images-are-not-loading-in-android-5-0-using-picasso-library

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