Using Android-UIL, How to stop downloading?
Asked Answered
V

1

6

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:

  1. 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:OutOfMemoryError stacktrace nostra uil
  2. 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.

Vital answered 4/2, 2015 at 14:30 Comment(11)
See also this issue on the project's GitHub.Vital
Are you sure your app doesn't have memory leaks?Circumspect
Until now, we haven't noticed any, and the issue reproduces only when using the paged views.Vital
@NOSTRA, I think that the memory leak / OOM is a side effect of us not really knowing how to stop / hold a download once its started. The difference between what most ppl would've done and us is that the images take a long time to render in the server, long enough for us to POLL the server for "tell me if an image is ready", and can't keep an open connection 'till then. So ho w can we stop an ImageDownloader once it's started, and can we abort that download and push it somehow back into the queue (LIFO/FIFO/MESS)?Vital
What about ImageLoader.cancelDisplayTask(...)?Circumspect
I think that it doesn't work if the ImageDownloader 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.Vital
I would like to see ReportPageDownloader sources.Circumspect
I've sent it by email. In Short (and in pseudocode): While (shouldKeepPolling){ if (timeout) throw new Exception(); if (pageIsReady) shouldKeepPolling = false; else WaitFourSeconds(); } DownloadPageImage(); Sorry linebreaks didn't work...Vital
I hardly can help you.Circumspect
Could you please share your getStream() method. Also Issue could be connected with viewPager. Which way to display on view pager do you use? Which method of UIL do you use - displayImage(), or loadImage(). Ther is difference between this two methods. Which min sdk do you use? About OOM, please read here developer.android.com/training/displaying-bitmaps/…Protasis
@TAC: I added Pseudo-Code to the question itself. I'm using a ViewPager, with an adapter, very similarly to what UIL does in the sample. I'm using displayImage(uri, imageView, options, listener). Can you describe the difference?Vital
P
1

Problem could be here 4. Download the image, return the stream. Downloading - it is main feature of UIL. Just return the NetworkStream here. Also I had OOM on UIL with large Images and enabled MemoryCache. Also which image size do you have on server. in load Image method you have control on Size. Also so helpful UIL GitHub please read 3 and 4 paragraph.

Protasis answered 8/6, 2015 at 14:21 Comment(2)
Sorry, I've probably threw you off, so I've edited the pseudocode better. I've read p3 and p4 on that github - the OOM is less important, I thought it was a side-effect of throwing an exception mid-getStream().Vital
I used to download images from AWS of Elastic Transcoder, so behaviour like your. i was returning null from getStream. It is still working. It has just warn by UIL logs. Also you could fing java doc on this method github.com/nostra13/Android-Universal-Image-Loader/blob/master/… throws IOException if some I/O error occurs during getting image stream throws UnsupportedOperationException if image URI has unsupported scheme(protocol)Protasis

© 2022 - 2024 — McMap. All rights reserved.