Test if a file is an image file

69,008

Solution 1

This works pretty well for me. Hope I could help

import javax.activation.MimetypesFileTypeMap;
import java.io.File;
class Untitled {
    public static void main(String[] args) {
        String filepath = "/the/file/path/image.jpg";
        File f = new File(filepath);
        String mimetype= new MimetypesFileTypeMap().getContentType(f);
        String type = mimetype.split("/")[0];
        if(type.equals("image"))
            System.out.println("It's an image");
        else 
            System.out.println("It's NOT an image");
    }
}

Solution 2

if( ImageIO.read(*here your input stream*) == null)
    *IS NOT IMAGE*    

And also there is an answer: How to check a uploaded file whether it is a image or other file?

Solution 3

In Java 7, there is the java.nio.file.Files.probeContentType() method. On Windows, this uses the file extension and the registry (it does not probe the file content). You can then check the second part of the MIME type and check whether it is in the form <X>/image.

Solution 4

You may try something like this:

String pathname="abc\xyz.png"
File file=new File(pathname);


String mimetype = Files.probeContentType(file.toPath());
//mimetype should be something like "image/png"

if (mimetype != null && mimetype.split("/")[0].equals("image")) {
    System.out.println("it is an image");
}

Solution 5

You may try something like this:

   import javax.activation.MimetypesFileTypeMap;

   File myFile;

   String mimeType = new MimetypesFileTypeMap().getContentType( myFile ));
   // mimeType should now be something like "image/png"

   if(mimeType.substring(0,5).equalsIgnoreCase("image")){
         // its an image
   }

this should work, although it doesn't seem to be the most elegant version.

Share:
69,008
lancegerday
Author by

lancegerday

I am a college student Majoring in Computer Engineering. I know Java, C++, C#, Python and have used SQL and asp.NET

Updated on July 18, 2022

Comments

  • lancegerday
    lancegerday almost 2 years

    I am using some file IO and want to know if there is a method to check if a file is an image?

  • GameDroids
    GameDroids about 12 years
    sorry, when I wrote my answer yours wasn't there... maybe it took me too long to write it. But you are right, its completely the same :)
  • Ismael
    Ismael about 12 years
    eheh, no problem. I think it happens to everyone all the time in here. :)
  • Stephen C
    Stephen C about 12 years
    @ismaelga - Indeed, it happens so often that most people wouldn't even bother to comment on it.
  • Ismael
    Ismael about 12 years
    @StephenC - I am a bad person, I know. I just commented because of the gap of something like 30 minutes, but i know it's still a normal thing. :) and i'm not being sarcastic
  • Ismael
    Ismael over 10 years
    hi @dermoritz This seems to be a problem on your system. Check out this SO post stackoverflow.com/questions/4855627/…
  • WowBow
    WowBow about 9 years
    How about if it is from URL? Before saving the URL to a file? I got "application/octet-stream" instead of image. Any idea ?
  • wangqi060934
    wangqi060934 almost 9 years
    @dermoritz,me too."new MimetypesFileTypeMap().getContentType(path)" returns "application/octet-stream",but "URLConnection.guessContentTypeFromName(path)" returns the right mime type.
  • gstackoverflow
    gstackoverflow almost 9 years
    Does it checks file content or just extension?
  • Alex Fedulov
    Alex Fedulov over 8 years
    This only checks based on the extension of the file. Non-valid images (other binaries without image content and proper file headers) cannot be detected using this method.
  • Ismael
    Ismael over 8 years
    You are right @Alex Fedulov. Try prunge s answer if this is not enough to you
  • Biagio Cannistraro
    Biagio Cannistraro about 8 years
    This is a bad way to test if the file is an image. a better solution is: Files.probeContentType(path);
  • Ismael
    Ismael about 8 years
    @BiagioCannistraro You might want to add that as another answer
  • yngwietiger
    yngwietiger almost 8 years
    On Linux, it seems to only look at the extension on my machine. Changed a .jpeg file to .txt and it returned "text/plain". I like Krystian's answer below.
  • zafar142003
    zafar142003 over 7 years
    This is the only way I could find if a file is an image file (on an Ubuntu system with Java 8).