0

we are getting the exception while reading the image,but i have an image in location path.Please suggest us?

byte photoContentByte[] = null;
BufferedImage originalImage = ImageIO.read(new File("D:/xyz/Repository/1234567890.tif"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpeg", baos);
baos.flush();
photoContentByte = baos.toByteArray();
baos.close();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
vcsred
  • 29
  • 2
  • 5
  • 1
    Where is the exception? If the problem is that `ImageIO.read` is returning null, we only really need that one line of code, and that piece of detail. – Jon Skeet Apr 13 '16 at 05:47
  • Shouldn't you be using a FileInputStream instead of a File? – djs Apr 13 '16 at 05:48
  • Old close duplicate: http://stackoverflow.com/questions/1954685 – Jon Skeet Apr 13 '16 at 05:48
  • i have tried with forward slash(\\) also – vcsred Apr 13 '16 at 05:52
  • For better help, always include the full stack trace of the error you see. Second, attach or link the image file you use for testing, for others to reproduce, if you believe it could be something special about this image file. – Harald K Apr 13 '16 at 08:51
  • PS: You should also mention (in the question) what TIFF plugin you are using to read the TIFFs, as that is relevant to fully reproduce your exception. – Harald K Apr 13 '16 at 11:59

1 Answers1

1

Try this:

BufferedImage originalImage = ImageIO.read(new FileInputStream("D:/xyz/Repository/1234567890.tif"));

EDIT:

As resolved in comments, you have a typo in file extension. It should be

BufferedImage originalImage = ImageIO.read(new File("D:/xyz/Repository/1234567890.tiff"));

i.e., the file extension needs to be tiff and not tif

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • @vcsred:- Can you try to check if you are getting the error for only `tif` image? I mean can you check for `jpeg` image? – Rahul Tripathi Apr 13 '16 at 05:54
  • Hi Rahul,i am able to read some tiff files and my requirement is need to read tiff file – vcsred Apr 13 '16 at 05:58
  • @vcsred:- Is the extension of your file correct? It should be `D:/xyz/Repository/1234567890.tiff`. You are missing the `f` – Rahul Tripathi Apr 13 '16 at 06:02
  • @vcsred Are you saying it was already correct with "tif" or that Rahul was correct with "tiff"? In any case, file extension is just part of the file name, and not used by `ImageIO`. It can be either, the only thing that matters is that it matches what you have stored on disk. – Harald K Apr 13 '16 at 09:07