I'm using UIL (by Nosotra) to download images that the server renders, and it takes the server up to 50 seconds to create each image. The server's timeout is set to 15 seconds, so we've created a polling mechanism that is implemented in the ImageDownloader.getStream()
method. After downloading, the images are displayed in a ViewPager (android.support.v4.view.ViewPager
, just as the sample does.)
When users navigate to other pages, I wish to stop the polling without downloading files, but it seems there's no "nice" way to break the downloading flow.
getStream Pseudo-code
1. Parse custom-style URI ("asdf://mypng|123455678945643563245");
2. Make a real world URL from it.
3. Poll the server for the image url (causes the server to render - could take up to 1m30s).
4. Get the stream from the URL, return the stream to caller.
Example Code: InputStream is = (InputStream) url.getContent();
What's tried so far
Returning null
from my getStream
method causes an NullPointerException
to be thrown, so it's basically same as just throwing an exception.
When an exception is thrown, the image does stop, but:
- A few images later, I get an
OutOfMemoryError
, so I show an error on screen. I shouldn't get the error. I already tried this SO question's checklist, but nothing worked. This is the OOM Stacktrace: - The downloading stops, and if the viewer won't recycle the view, when returning to that page, I'll still see that error page (no retry).
What I wish
I wish for me to have a "wait" method that would add stop the current download and re-add a new task to the end of the queue (currently on set on a QueueProcessingType.LIFO
, and I wish it to download the re-added after after current pages, and any new pages the user wants should have precedence over the re-added ones).
I would also settle for avoiding the OutOfMemoryError
.
Please help.
ImageLoader.cancelDisplayTask(...)
? – CircumspectImageDownloader
has already started (our implementation of it,ReportPageDownloader
does the polling). I Voted you up because there's a good chance it will help many others. – VitalReportPageDownloader
sources. – CircumspectWhile (shouldKeepPolling){
if (timeout) throw new Exception();
if (pageIsReady) shouldKeepPolling = false; else WaitFourSeconds();
}
DownloadPageImage();
Sorry linebreaks didn't work... – VitaldisplayImage(uri, imageView, options, listener)
. Can you describe the difference? – Vital