7 Replies Latest reply on Jan 26, 2010 6:59 AM by 807580

    How to check if a file represents an image file

    807580
      How to check if a file represents an image file, I need this to validate if a file is an image file?
      I am cheking the file extn using regular expression but it doesn't seems sufficient as if I change an image abc.png to abc.xyz,
      still i can display the image on the button. Any suggestions?
        • 1. Re: How to check if a file represents an image file
          793415
          You might try an [ImageIO.read(File)|http://java.sun.com/javase/6/docs/api/javax/imageio/ImageIO.html#read(java.io.File)] *(<- link)* on the File. Hopefully it should throw an IOException if the data is not an image.

          Another way is to read the first bytes of the file and see if they represent particular image file formats.
          • 2. Re: How to check if a file represents an image file
            807580
            I am cheking the file extn using regular expression but it doesn't seems sufficient as if I change an image abc.png to abc.xyz,
            still i can display the image on the button. Any suggestions?
            What is it for a file to represent an image?

            You are quite right that merely checking an extension won't give you a foolproof check. (Your example shows that having a well known extension is not necessary but by renaming foo.txt to foo.png we can see that it isn't sufficient either.)

            If I had mime information available I might use that. Or I'd go with checking the extension. Is there some reason why this won't do? Because in that case we are back to the first question.
            • 3. Re: How to check if a file represents an image file
              807580
              Thanks for your responces, I tried reading my file using
              ImageIO.read(file);
              it won't work, All my image files are inside the jar icons.jar that could be a case. I used
              ImageIcon retVal = new ImageIcon(url);
              retVal.getImageLoadStatus() == MediaTracker.COMPLETE
              to check if my file is loaded as an image. it returns me correct results. but now I have got another issue i.e. if I specify a directory name in the
              url (rather than the file name) to ImageIcon constructor, it hang and after sometime it throws me the following exception:
              Uncaught error fetching image:
              java.lang.NullPointerException
                   at java.io.FilterInputStream.markSupported(FilterInputStream.java:217)
                   at sun.awt.image.InputStreamImageSource.getDecoder(InputStreamImageSource.java:191)
                   at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:146)
                   at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240)
                   at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
                   at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
              I also used

              File file = new File(url.getPath())

              and query on file object for the following methods but they all returns false even file a image file. I think this is because my image files are inside the jar.
              file.exists(), file.canRead(), file.isDirectory() or file.isFile()

              Is there a way to know if the path belongs to a file or directory if the they are inside the jar?,
              I would prefer if I need to open the jar for this, because of performance hit.
              • 4. Re: How to check if a file represents an image file
                793415
                sumitkchauhan wrote:
                ...now I have got another issue i.e. if I specify a directory name in the
                url (rather than the file name) to ImageIcon constructor,
                Why would ImageIcon have a constructor that accepts a directory? What is ImageIcon supposed to do with a directory?
                Is there a way to know if the path belongs to a file or directory if the they are inside the jar?,
                Sure, but you really have me confused about what you are trying to achieve here.
                I would prefer if I need to open the jar for this, because of performance hit.
                What does that mean?
                • 5. Re: How to check if a file represents an image file
                  807580
                  I am sorry to confuse you, let me simplify the problem.

                  Is there a way to check if a file is directory or file, if the file exists in a jar?

                  I tried using file object as I mentioned above but it won't work.
                  • 6. Re: How to check if a file represents an image file
                    793415
                    sumitkchauhan wrote:
                    ...Is there a way to check if a file is directory or file, if the file exists in a jar?
                    I just realised that:
                    a) This is an entirely different question to your subject line.
                    b) Neither the original or new questions has anything to do with Swing.
                    I tried using file object as I mentioned above but it won't work.
                    <DWS>Maybe it is just sleepy. Try giving it a good night's rest, it might work in the morning.</DWS>

                    But sure. Establish a ZipFile, then call [ZipFile.entries()|http://java.sun.com/javase/6/docs/api/java/util/zip/ZipFile.html#entries()]. Iterate the ZipEntrys and call the [ZipEntry.isDirectory()|http://java.sun.com/javase/6/docs/api/java/util/zip/ZipEntry.html#isDirectory()] method.
                    • 7. This Thread is now moved
                      807580
                      Note: This thread was originally posted in the Swing forum, but moved to this forum for closer topic alignment.