2

I need to send image from android app to java app. Basically, I need a byte array from the image to send to rf module which transmits.Another rf module receives and sends the byte array to java app which must make the image .

Android code:

FileInputStream fis = new FileInputStream(myFile);
    byte[] b=new byte[(int)myFile.length()];        
    fis.read(b);server.send(b);

Java code:

FileOutputStream fwrite = new FileOutputStream(new File("my_xml"),true);
                                fwrite.write(bb);//bb is a byte from rf using input stream as soon as a byte comes it is read to file. This is necessary for some other reasons
                                fwrite.flush();
                                fwrite.close();

After getting full file:

FileInputStream fir=new FileInputStream("my_xml");
        final BufferedImage bufferedImage = ImageIO.read(fir);
        ImageIO.write(bufferedImage, "bmp", new File("image.bmp"));
        fir.close();

I am getting error javax.imageio.IIOException: Bogus Huffman table definition The rf is working fine because text file is being sent perfectly.Please help.Even without ImageIo code is not giving image even after changing extension to jpeg

Samyuktha
  • 31
  • 5

2 Answers2

0

The error means that the image file cant be read because the format is wrong.That is some bytes are missing or wrong or out of proper position and therefore file cant be decoded. My rf transfer does not have protocols like tcp/ip therefore some bytes are lost due to error in communication channel and hence the error.

Samyuktha
  • 31
  • 5
0

You don't need to use ImageIO just to copy a file. Just read and write the bytes.

Your code has other problems:

  1. You are assuming that read(byte[]) fills the buffer. It doesn't. Check the Javadoc.

  2. You are also assuming that the file length fits into an int. If it does, fine. If it doesn't, you are hosed.

  3. You appear to be opening and closing the FileOutputStream on every byte received. This could not be more inefficient. Open it once, write everything, close it.

  4. flush() before close() is redundant.

  5. You are storing the image in a file called 'my_xml'. This is only going to cause confusion, if it hasn't already.

  6. You don't even need the file. Just load the image directly from the input stream.

user207421
  • 305,947
  • 44
  • 307
  • 483