Skip to content Skip to sidebar Skip to footer

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).openC

Solution 1:

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.

Solution 2:

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

Post a Comment for "Interrupting Bitmapfactory.decodestream"