user818700
user818700

Reputation:

Image is null after creation with not null InputStream

I have the following code:

public BufferedImage inputStreamToImage (InputStream stream) {        

    BufferedImage image = null;

    if (stream != null ) {

        try {
            image = ImageIO.read(stream);
            // image is still null here <--
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    return image;
}

I test if the InputStream is null, which it's not. I then create a new BufferedImage from the InputStream. Again nothing goes wrong and code executes like it's working. But then when I try and use the image it's null.. In other words, right after a "successful" creation I test the image and it's null.

EDIT

I upload a .tif file via a html form like so:

<form action="webresources/read" method="post" enctype="multipart/form-data">

    <p>
    Select a file : <input type="file" name="file" id="file" />
    </p>

    <input type="submit" value="Upload It" />

</form>

Any help? Thanks in advance :)

Upvotes: 1

Views: 539

Answers (3)

user818700
user818700

Reputation:

Ah! So.. The reason is that ImageIO.read() does not support .tif images. I tried it with a .jpg and it worked fine.

Upvotes: 1

rossa
rossa

Reputation: 119

Is it possible to have the following case stated in the documentation of ImageIO - "The InputStream is wrapped in an ImageInputStream. If no registered ImageReader claims to be able to read the resulting stream, null is returned."?

Upvotes: 0

Jean Logeart
Jean Logeart

Reputation: 53829

From the javadoc:

If no registered ImageReader claims to be able to read the resulting stream, null is returned.

You InputStream probably does not exactly contain a readable image.

Upvotes: 2

Related Questions