Search code examples
androidandroid-activityuniversal-image-loader

How to stop download of a single image


I'm creating an application where the user selects one item from a recycler view and this starts the download of a group of images.

I'm using universal image loader's loadImage method to fetch images for me which is working very well.

The problem is,if the user wants to cancel downloading this group of images (can be very network consuming) I cannot find a way to make ui stop only some of the images.

I'm aware of the method ImageLoader.cancelDisplayTask(), but as I'm using loadImage I have no ImageView or ImageAware available to send as a parameter to this task.

Also ImageLoader.stop() or ImageLoader.pause() stops all current downloads and I want to stop only the ones selected by the user.

How can I achieve this?

PS: A suggestion to some future development would be returning a pointer of the download task when using loadImage method. Maybe I'll look it myself when I have some time to contribute to this awesome library.

Here's my code for downloading an image.

private void downloadFile(final URL downloadURL,
                          final URL destinationURL,
                          final boolean isThumbnail,
                          final MyDownloadListener listener) {
    final NewsData thisClass = this;
    final ImageLoader il = ImageLoader.getInstance();

    File imageFile = il.getDiskCache().get(destinationURL.toString());
    if (imageFile.exists()) {
        fallDownloadCompleted(this, isThumbnail);
        return;
    }
    il.stop();
    il.loadImage(
            downloadURL.toString(),
            null,
            Application.getAppDisplayOptions(),
            new SimpleImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    if (listener != null) {
                        listener.onLoadingStarted(thisClass);
                    }
                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    fallDownloadFail(thisClass, failReason, isThumbnail);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    try {
                        il.getDiskCache().save(destinationURL.toString(), loadedImage);
                        if (isToday()) deleteImageFile(imageUri);
                    } catch (IOException e) {
                        deleteImageFile(imageUri);
                        fallDownloadFail(
                                thisClass,
                                new FailReason(FailReason.FailType.IO_ERROR, new Throwable()),
                                isThumbnail);
                        return;
                    }

                    fallDownloadCompleted(thisClass, isThumbnail);
                }
            },
            new ImageLoadingProgressListener() {
                @Override
                public void onProgressUpdate(String imageUri, View view, int current, int total) {
                    float progress = 0;
                    if (total > 0 && numberOfTotalPages > 0) {
                        if (!isThumbnail) {

                            progress = numberOfAvailablePages / (float) numberOfTotalPages;
                            progress += (current / (float) total) / numberOfTotalPages;

                        } else {
                            progress = current / (float) total;
                        }
                    }
                    if (listener != null) {
                        listener.onProgressUpdate(thisClass, progress);

                    }
                }
            });
}

Solution

  • I managed to solve this issue with a small workaround.

    I couldn't find a way to cancel a particular ImageLoader.loadImage() so what I had to do was use the normal ImageLoader.displayImage() by passing a NonViewAware class in place of the image view required as a parameter.

    I then added this NonViewAware into an ArrayList<NonViewAware> to keep track of which images were being downloaded.

    Finally when I wanted to cancel a display task I just called ImageLoader.cancelDisplayTask(NonViewAware) with one of the stored items from the list.