This content has been marked as final. Show 7 replies
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.
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,What is it for a file to represent an image?
still i can display the image on the button. Any suggestions?
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.
Thanks for your responces, I tried reading my file using
it won't work, All my image files are inside the jar icons.jar that could be a case. I used
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
ImageIcon retVal = new ImageIcon(url); retVal.getImageLoadStatus() == MediaTracker.COMPLETE
url (rather than the file name) to ImageIcon constructor, it hang and after sometime it throws me the following exception:
I also used
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)
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.
sumitkchauhan wrote:Why would ImageIcon have a constructor that accepts a directory? What is ImageIcon supposed to do with a directory?
...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,
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?
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.
sumitkchauhan wrote:I just realised that:
...Is there a way to check if a file is directory or file, if the file exists in a jar?
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.