Android Universal Image Loader requests with same URL are being cancelled
Asked Answered
M

2

9

I am trying to load images to the bitmaps using UIL. I need to load multiple images and I have noticed that in some cases image urls can be the same. And in such cases only first image is loaded. How to avoid request cancelling in UIL?

Code is run 3 times in the loop:

ImageSize targetSize = new ImageSize(70, 70);
ImageLoader.getInstance().loadImage("http://icons.iconarchive.com/icons/yellowicon/game-stars/256/Mario-icon.png", targetSize, new ImageLoadingListener() {
    @Override
    public void onLoadingStarted(String imageUri, View view) {
        Log.e("tag", "onLoadingStarted");
    }

    @Override
    public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
         Log.e("tag", "onLoadingFailed");
    }

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        Log.e("tag", "onLoadingComplete");
    }

    @Override
    public void onLoadingCancelled(String imageUri, View view) {
        Log.e("tag", "onLoadingCancelled");
    }
});

Logs are:

onLoadingStarted
onLoadingStarted
onLoadingStarted
onLoadingComplete
onLoadingCancelled
onLoadingCancelled
Mccrary answered 18/11, 2014 at 19:15 Comment(1)
change from loadImage to displayImage and see if it works. If you are loading to the same imageview, the loading will be cancelled if another task is processing that imageviewBobble
T
15

UIL cancels previous displayImage(...) task for the same ImageView.

UIL cancels previous loadImage(...) task for the same URL.

Actually for both cases UIL operates with ImageAwares inside and compare ImageAware.getId() to decide to cancel task or not.

In your case to prevent task cancelling you can do like this:

ImageSize targetSize = new ImageSize(70, 70);
ImageAware imageAware = new NonViewAware(targetSize, ViewScaleType.CROP);
ImageLoader.getInstance().displayImage("http://icons...", imageAware, ...);
Tedda answered 18/11, 2014 at 20:12 Comment(4)
but how for: loadImage(...)?Depopulate
Internally loadImage(...) is displayImage(...) using NonViewAware.Tedda
Thanks, this saved my life. Although it would be nice to have the option in ImageLoaderConfig to prevent cancelling. Since I've encountered this issue several times, and I'm sure others as well.Probative
Please change the arithmetic operator to assignment operator ImageAware imageAware - new NonViewAware(targetSize, ViewScaleType.CROP);Theomorphic
K
0

Hi I had also same problem with several same uri during calling same time. I have developed Custom class which uses ImageLoader and solves the problem

public static MyMusicImageLoader myMusicImageLoader;
public ImageLoader imageLoader;
public Map<String,MImageLoader> mImageLoaderMap = new HashMap<String, MImageLoader>();

public static MyMusicImageLoader getInstance(){
    if(myMusicImageLoader==null)
        myMusicImageLoader = new MyMusicImageLoader();
    return myMusicImageLoader;
}

public MyMusicImageLoader(){
    imageLoader = ImageLoader.getInstance();
}

public void loadImage(final String url,ImageLoadingListener imageLoadingListener){

    MImageLoader mImageLoader = mImageLoaderMap.get(url);
    if(mImageLoader==null){
        mImageLoader = new MImageLoader(url);
        mImageLoader.addImageLoadingListener(imageLoadingListener);
        mImageLoaderMap.put(url,mImageLoader);
        imageLoader.loadImage(url, new SimpleImageLoadingListener(){
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                setAllMImageLoader(url,loadedImage);
            }
        });
    }else{
        mImageLoader.addImageLoadingListener(imageLoadingListener);
    }

}

public void setAllMImageLoader(String url, Bitmap bitmap){
    MImageLoader mImageLoader =  mImageLoaderMap.remove(url);
    ArrayList<ImageLoadingListener> imageLoadingListeners = mImageLoader.imageLoadingListeners;

    for(int i=0; i<imageLoadingListeners.size(); i++){
        ImageLoadingListener imageLoadingListener = imageLoadingListeners.get(i);
        imageLoadingListener.onLoadingComplete("",null,bitmap);
    }

    imageLoadingListeners.clear();

}

class MImageLoader{
    String imageUrl;
    ArrayList<ImageLoadingListener> imageLoadingListeners = new ArrayList<ImageLoadingListener>();
    MImageLoader(String imageUrl){
        this.imageUrl = imageUrl;
    }

    void addImageLoadingListener(ImageLoadingListener imageLoadingListener){
        imageLoadingListeners.add(imageLoadingListener);
    }
}
Kollwitz answered 5/8, 2015 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.