Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple calls with the same URL only gets 1 answer #510

Closed
cdongieux opened this issue Jan 14, 2014 · 12 comments
Closed

Multiple calls with the same URL only gets 1 answer #510

cdongieux opened this issue Jan 14, 2014 · 12 comments
Labels

Comments

@cdongieux
Copy link

Hi,

First of all, thank you so much for all your remarkable work, your lib really rocks!

I'm currently intregrating it in my project, and I found something strange. See the following snippet:

    private class Networker {
        void getStuff(String source) {
            ImageLoader.getInstance().loadImage(source, new SimpleListener());
        }

        private class SimpleListener extends SimpleImageLoadingListener {
            @Override
            public void onLoadingComplete(String imageUri, View view,
                    Bitmap loadedImage) {
                Log.d("UIL", "onLoadingComplete: "+imageUri);
            }
        }
    }

    private void func() {
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(false).cacheOnDisc(false).build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext()).defaultDisplayImageOptions(
                defaultOptions).build();
        ImageLoader.getInstance().init(config);

        Networker n = new Networker();
        n.getStuff("http://www.google.fr/images/srpr/logo11w.png");
        n.getStuff("http://www.google.fr/images/srpr/logo11w.png");
        n.getStuff("http://www.google.fr/images/srpr/logo11w.png");
    }

When calling method func(), "OnLoadingComplete" is only called once. What am I missing?

Thanks in advance.

Regards,
Christophe.

@nostra13
Copy link
Owner

Which lib version do you use?

@cdongieux
Copy link
Author

The latest one: 1.9.1

@nostra13
Copy link
Owner

Ok, 1.9.1 introduces following change: "loadImage(...) call cancels previous task for the same image URI (#475)"

In your case you should create ImageNonViewAware (with null as first param in constructor) and pass it into displayImage() method.

ImageSize imageSize = ...;
ImageNonViewAware imageAware = new ImageNonViewAware(imageSize, ViewScaleType.CROP)
ImageLoader.getInstance().displayImage(source, imageAware, new SimpleListener());

@toko74
Copy link

toko74 commented Mar 13, 2014

Hi,
Great library.
I'm having the same problem (on 1.9.1). I couldn't find the loadImage(ImageAware, ImageLoadingListener) method.
Did you mean: displayImage(uri, ImageAware, ImageLoadingListener)?
Anyway, Is this going to be fixed? I guess onLoadingComplete should be called on every loadImage(...) without adding CustomImageNonViewAware.

@nostra13
Copy link
Owner

Yes, you're right, you should use displayImage() method. Also I edited my answer so you shouldn't extend ImageNonViewAware, just use it.

Is it going to be fixed? I think, no. Both variants (one common callback for the same URIs or separate callbacks for every call with the same URI) have their pros and cons but I decided to choose 1st one because this behavior of loadImage() matches to displayImage()'s behavior, also it prevents memory consumption by default.
This issue source is here - #475

@toko74
Copy link

toko74 commented Mar 13, 2014

I was thinking the idea behind loadImage(...) is getting the bitmap in the callBack without the need to give ImageView. Base on that I think the solution should be separate callbacks for every call even with the same URI (you don't need to send the same requests again, just hold it until the first request is back an send all callbacks).
In addition the current loadImage(...) behaves differently between first time (Images not in cache - just one callback of each URI) and second time (Images in cache - all callbacks arrive)

@nostra13
Copy link
Owner

Actually "common callback" isn't correct term, I shouldn't use it.
I meant only last callback got onLoadingComplete(). All previous calls got onLoadingCancelled() in their callbacks. That's the point.
There are some reasons for current behavior of loadImage() and I propose to deal with it.

If you want the following loadImage() calls for the same URI don't cancel previous loadImage() call for that URI then you should use aforementioned workaround.
Replace call

imageLoader.loadImage(imageUri, listener)

with

ImageSize imageSize = new ImageSize(width, height);
ImageNonViewAware imageAware = new ImageNonViewAware(imageSize, ViewScaleType.CROP)
ImageLoader.getInstance().displayImage(imageUri, imageAware, listener);

Or you can change ImageNonViewAware.getId() sources:

@Override
public int getId() {
    return super.hashCode();
}

and continue use loadImage() method.

@Frank1234
Copy link

And what if you do not know the target size?

@Yonfu
Copy link

Yonfu commented Feb 2, 2015

You can take the absolute width of the display in pixels. See method

    loadImage(String uri, ImageSize targetImageSize, DisplayImageOptions options,
            ImageLoadingListener listener, ImageLoadingProgressListener progressListener)
        checkConfiguration();
    if (targetImageSize == null) {
        targetImageSize = configuration.getMaxImageSize();  
    }
        ...

Method getMaxImageSize

    ImageSize getMaxImageSize() {
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();

        int width = maxImageWidthForMemoryCache;
        if (width <= 0) {
            width = displayMetrics.widthPixels;
        }
        int height = maxImageHeightForMemoryCache;
        if (height <= 0) {
            height = displayMetrics.heightPixels;
        }
        return new ImageSize(width, height);
    }

@Frank1234
Copy link

Thank you.

@nebyan
Copy link

nebyan commented Aug 5, 2015

Hi I also had the same problem have wrote custom class it use Imageloader find good solution.

public class MyMusicImageLoader {

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

}

markusheilig added a commit to locator-kn/android-app that referenced this issue Jan 6, 2016
@PoetKing
Copy link

Maybe it could help some people:
https://mp.weixin.qq.com/s/dFdKg5CDVUXktQiE9p9Kcg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants