javaandroidmultithreadinginputstreambitmapfactory

Interrupting BitmapFactory.decodeStream


Let's assume we have a worker thread which calls BitmapFactory.decodeStream with some connection stream, like this:

Bitmap bitmap = BitmapFactory.decodeStream(new URL(imgUrl).openConnection().getInputStream(), null, someUnrelatedOptions);

Now we need to terminate this thread immedietely in order to reclaim any memory that BitmapFactory has allocated. We can't wait for it to finish for too long, as the memory is required by UI thread. The example is simplified, so let's don't go into details why it has to be done this way. I'm looking for a solution to this approach, not for another approach.

We can't brutally kill the thread, as it's doing some other things too and there's a chance we will leak some stuff. The only clean way to go is to use workerThread.interrupt().

Unfortunetely, the BitmapFactory doesn't care whether the thread was interrupted and continue to do it's work. Obviously I'm checking the Thread.currentThread().isInterrupted() right after it exists, but it's not sufficient. On really slow internet connections it may take years to load the image which will freeze the application (remember, UI thread is waiting).

So the question is - how do I make BitmapFactory stop when the thread is interrupted? My idea is to wrap the InputStream in custom FilterInputStream, overwrite every function (there aren't many, actually) and in each of them check whether the thread is interrupted. If yes, throw some IOException. This way BitmapFactory will stop as soon as it tries to pull some data from the InputStream and it should be enough. Are there any better ways? If no, should I call close() on the stream before throwing IOException?

And a little note: the UI thread that is waiting for additional memory is displaying a nice ProgressBar, so it's not a problem if it takes a second or two for the worker thread to terminate.

Thanks!


Solution

  • I think you can call someUnrelatedOptions->requestCancelDecode() in the UI thread to cancel the decoding, although it is not guaranteed to cancel the decode.

    Google keywords "bitmapfactory requestCancelDecode decodeStream url openconnection" (without the quotes) you can find sample code, for example in Android Gallery3D app.

    Regards

    Ziteng Chen