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