Search code examples
androidbitmapfactory

decodeByteArray() returns null roughly 90% of the time


I am trying to display an image sent over a local network to my android device. An image is sent from a computer via tcp to my android device. The image is a png. The data sent over is a byte array stream of the png, which is packaged into a google protobuf message. On the android side, when receiving the data, it is read into a byte array, and the array is then given to BitmapFactory.decodeByteArray(). However, this returns null roughly 90% of the time. This only happens on a real device, but I can only test on the htc incredible at the moment. I've tried this on the android sdk emulator and i can get my image 100% of the time.

Other issues relating to BitmapFactory online has always been related to using file streams, where decoding isn't getting the entirety of the data, but I have yet to find any solutions for when the developer is sure the entirety of the data is received, and it only happens on a real device.

Is there some type of usage that I am unaware of for decodeByteArray()? The byte[] I am passing in is just the file itself.

Edit: Resolved thanks to a second insight from Brian Cooley.

I was just too quick to judge that the error may be in decodeByteArray(). If anyone ever runs into this problem do make sure that you make sure you have the data in its entirety. I made the false assumption that my byte stream was good. So first do a quick diff of the data you're sending and receiving, and make sure you're getting what you should.


Solution

  • One thing you might try is writing the raw bytes to a file on the SD card and looking at it on your computer, like was suggested for this question

    This would reveal whether the problem is in the file or in your code. Since you are not seeing the problem in the emulator, my guess is that it is related to downloading the data over the phone.