1

This code works quite well but it only detects JPG and not PNG

public String[] getImageFileList(String path_to_directory){
        File file = new File(path_to_directory);
        if(!file.exists())return null;
        String[] images = file.list(new FilenameFilter() {
              @Override
              public boolean accept(File current, String name) {
                boolean result;
                File file = new File(current, name);
                String mimeType = new MimetypesFileTypeMap().getContentType(file).split("/")[0];
                result = mimeType.equals("image");
                return result;
              }
            });
        return images;
    }

How can I make it detect PNG images too?

EDIT: When checking closer the "result" will become "image" for JPG and "application" for PNG. Am I right in guessing that the PNG is not the only file-type that has that "application" mimeType?

Chikage
  • 281
  • 1
  • 3
  • 11
  • [Perhaps this answer helps?](http://stackoverflow.com/questions/21133581/detect-if-given-file-is-image-and-is-valid-image-of-specific-type-in-java) – gtgaxiola Apr 08 '15 at 19:07
  • The correct MIME type for PNG is "image/png". Your `MimetypesFileTypeMap()` function needs fixing or updating. – Lee Daniel Crocker Apr 09 '15 at 20:23

3 Answers3

2

The problem can be solved by defining the file-types you want to look for to the MimetypesFileTypeMap like this:

MimetypesFileTypeMap mtftp = new MimetypesFileTypeMap();
mtftp.addMimeTypes("image png jpg jpeg")

The resulting functional code then becomes like this:

public String[] getImageFileList(String path_to_directory){
        File file = new File(path_to_directory);
        if(!file.exists())return null;
        String[] images = file.list(new FilenameFilter() {
              @Override
              public boolean accept(File current, String name) {
                boolean result;
                File file = new File(current, name);
                MimetypesFileTypeMap mftp = new MimetypesFileTypeMap();
                mftp.addMimeTypes("image png jpg jpeg");
                String mimeType = mftp.getContentType(file).split("/")[0];
                result = mimeType.equals("image");
                System.out.println(mimeType);
                return result;
              }
            });
        return images;
    }
Chikage
  • 281
  • 1
  • 3
  • 11
1

Normally, the mime types for files are:

PNG: image/png
JPG: image/jpg
...

You can find the complete list here.

At your code, while checking the mime types, I think you have got some png files that are not represented as "image/png" instead I think they are "application/octet-stream" or something like that. When you upload or download a file over the internet, the file can get the mime type as "application/octet-stream" on the net.

Or,

Your application just can't get the proper mime types for the files.

At this answer, it is indicated that if you do not set the file types properly to your MimetypesFileTypeMap instance, you may always get the mime type as "application/octet-stream". So the solution is setting the needed file types to the MimetypesFileTypeMap instance like this:

MimetypesFileTypeMap mtftp = new MimetypesFileTypeMap();
mtftp.addMimeTypes("image png jpeg jpg");

Then I think your program will start to detect the mime type of the png file correctly.

Community
  • 1
  • 1
Bahadir Tasdemir
  • 10,325
  • 4
  • 49
  • 61
0

If you want to detect image files, you need to read the first few bytes and detect their signature. Most image formats, including JPEG and PNG, have distinctive structures at the file header.

user3344003
  • 20,574
  • 3
  • 26
  • 62