2012-05-06

BitmapFactory.decodeStream(InputStream)

I desired to generate a new bitmap image with a subclass of java.nio.Buffer for some reason.
I found a simple way to convert Buffer into InputStrem in a post on StackOverFlow titled "java - Wrapping a ByteBuffer with an InputStream"(http://stackoverflow.com/questions/4332264/wrapping-a-bytebuffer-with-an-inputstream). However, I encountered a NPE with the below message on my logcat after integrating my own wrapping ByteBuffer class.
SkImageDecoder::Factory returned null

I investigated this problem and found an essential reason of the message.



The document for android developers from Google doesn't describe prerequisites for using decodeStream(InputStream) method, but the method obviously provided for files. So headers are needed. The debug message was outputted due to lack of headers (If you'd like to check the procedure the message is made, download the whole Android source code and read frameworks/base/core/jni/android/graphics/BitmapFactory.cpp. This source chooses which decoder should deal the InputStream by the result of processing headers in the InputStream, and when an appropriate decoder is not found, it returns null. If you'd like to your Buffer deal as a Bitmap, you must put "BM" as the first element).

This is why I decide not to use a subclass of Buffer but use Bitmap#setPixel.

No comments:

Post a Comment