public static boolean isImage ( File file ) { try { String mimeType = Files . probeContentType ( file . toPath ( ) ) ; if ( mimeType == null ) { // In some OS there is a bug where it always returns null/ String extension = FilenameUtils . getExtension ( file . getName ( ) ) . toLowerCase ( ) ; String [ ] suffixes = ImageIO . getReaderFileSuffixes ( ) ; for ( String s : suffixes ) { if ( s . equals ( extension ) ) { return true ; } } } else { return mimeType . startsWith ( "image" ) ; } } catch ( IOException ignore ) { } return false ; }
Uses mime type to determine if it s an image or not . If mime fails it will look at the suffix . This isn t 100% correct .