Interrupting BitmapFactory.decodeStream

◇◆丶佛笑我妖孽 提交于 2020-01-15 11:39:13

问题


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!


回答1:


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




回答2:


I was getting sigsegv errors on ART when closing the inputstream of Bitmap.decodeStream during its native processing of very large jpeg assets. This function is essential due to its use of very powerful (and volatile) native decoders.

I solved it by first wrapping the input stream in a BufferedInputStream, then passing that to decodeStream.

To reliably interrupt decodeStream, close the BufferedInputstream (bis) thusly:

bis.mark(0); //Otto von?
bis.reset();
bis.close();

This will immediately and brutally halt the native decoder, generating a stacktrace from decodeStream which you can catch (no crash).

The underlying issue is with the native decoder, and resetting the stream prior to a premature close operation seems to solve the memory related crash.

There should be an obvious way to reliably interrupt the decoder, this took way too much frenzied experimentation to workaround. Hopefully it will be solved in the native layer.



来源:https://stackoverflow.com/questions/11993645/interrupting-bitmapfactory-decodestream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!