1

I'm reading a file that has an array of bytes. I downloaded the Apache Commons IO library to use the FileUtils' method readFileToByteArray

File file = new File("/home/username/array.txt");
FileUtils fu = new FileUtils();
byte[] array = FileUtils.readFileToByteArray(file);

I want to convert the array of bytes to an Image.

ByteArrayInputStream bis = new ByteArrayInputStream(array);
Iterator<?> readers = ImageIO.getImageReadersByFormatName("gif");

ImageReader reader = (ImageReader) readers.next();
Object source = bis;

ImageInputStream iis = ImageIO.createImageInputStream(source);

reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();

Image image = reader.read(0, param); // this line is the problem

When the code goes to the referred line, it throws an Exception saying

javax.imageio.IIOException: Unexpected block type 128!

I don't know what this exception means, therefore, I don't know how to fix it. Any further information that could be helpful just need to be requested.

Thanks

1
  • You can do all of the above with a single line of code: Image image = ImageIO.read(file); That will eliminate any issues with byte arrays and will remove your dependency on an external library. Commented Feb 18, 2013 at 14:47

4 Answers 4

1

I've tried your code on this file and it works fine.

What's the format of your array.txt? readFileToByteArray() expects a binary format, and your image reader will further expect it to be a GIF file.

Sign up to request clarification or add additional context in comments.

5 Comments

How did you get the byteArray of it? Maybe what I'm getting isn't a byteArray.
Well, I suppose the question is, what does your file look like e.g. if opened in an editor. The .txt is worrying. If your file looks like 123 22 55 98..., then you'll need to parse the values e.g. with a Scanner.
Well, the only string readable is GIF89a. The rest is unreadable by any means.
Well, in that case it should work. If you change the file extension to .gif and double-click it in your file browser, does it open normally? If not, it's not impossible your image file is corrupt.
Yeah, I couldn't open it. I'll check my methods of getting the bytearray than come back to the question. Thanks.
1

Once you have byte[] you can use ImageIO to write it to BufferedImage.

BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(array));
ImageIO.write(bImageFromConvert, "gif", new File("c:/test.gif"));

1 Comment

I tried your approach, but the result was the same exception.
1

That code means the reader couldn't decipher the metadata on the image file. Make sure the right file is getting read and it's well formed. Or it may be expecting a different file type.

6 Comments

How can I assure the file is well formed?
See if that image file can be opened by another program. Preferably one that's not overly forgiving or one that allows you to inspect the metadata.
Actually, I don't have the image. What I have is a txt file with the data information written inside.
As I and the other responders have observed, we think the problem lies in the data file. Your code clip implies a text file, the reader expects a binary file. If you can open it up and read numbers, then it's, at best, an ASCII encoded file that will need to be converted back to binary before the Imagereader can consume it.
No. As I answered on Vlad's post, I don't view numbers, only a bunch of interrogation marks and some letters and numbers mixed up.
|
0

without byte[] i feel this will be good for multipart file transfer,for this we need apache common jar files

    final FileOutputStream output = new FileOutputStream("D:\\Dir\\"+ request.getParameter("imageName") + ".jpg");
        IOUtils.copy(request.getPart("file").getInputStream(), output);
        output.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.