ImageIO and BitmapFactory.decodeByteArray

All your problems with Audio, Video and Images.

ImageIO and BitmapFactory.decodeByteArray

Postby Deathball » Thu Mar 04, 2010 10:37 pm

Hi.

I want to send an Image (.png or .jpg, I tried both) with an Socket Connection from a server to an Android phone.
I convert the Buffered Image to a byte array via:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         ByteArrayOutputStream stream = new ByteArrayOutputStream();
  3.  
  4.         ImageIO.write(img, "png", stream);
  5.  
  6.         return stream.toByteArray();
  7.  
  8.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


The server has no problems with decoding it again. I suspected that the following would be enough to convert the Byte Array back to an image on the phone:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         byte[] data = ...
  3.  
  4.         Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
  5.  
  6.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


But unfortunately the BitmapFactory returns null, indicading that the byte array could not be decoded.

I'm clueless here, I suspect the decoding to be the problem here, but of course it could also be the socket connection. :?

That's why I wanted to ask, if someone can confirm that a byte array produced by ImageIO/ByteArrayOutputStream can be decoded with BitmapFactory.decodeByteArray or if someone knows a better way of doing this.

Thx in advance
Deathball
Freshman
Freshman
 
Posts: 2
Joined: Thu Mar 04, 2010 10:12 pm

Top

Postby didi_yeah » Tue Mar 09, 2010 7:40 pm

I have the same problem
That's mean a byte[] representative of my image but the BitmapFactory.decodeByteArray answers null
Please some help if you know why :)
didi_yeah
Freshman
Freshman
 
Posts: 2
Joined: Tue Mar 09, 2010 7:37 pm

Postby didi_yeah » Wed Mar 17, 2010 1:07 am

I figured out the problem but I don't know if that could help Deathball
In fact, I gave to my parsing function this next inputStream : entity.getContent
I had to consume completely it before parsing the stream and it works :)

Bye
didi_yeah
Freshman
Freshman
 
Posts: 2
Joined: Tue Mar 09, 2010 7:37 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Exabot [Bot] and 21 guests