23

Possible Duplicate:
Know if a file is a image in Java/Android

How can I check a file if it is an image? like following:

if(file.isImage)....

If it's not possible with standard libraries, how can I do it with the MagickImage lib?

Thanks in advance!

Community
  • 1
  • 1
Marco Seiz
  • 944
  • 5
  • 19
  • 40

2 Answers2

50

I think if you want to check whether a file is an image, you need to read it. An image file may not obey the file extension rules. You can try to parse the file by BitmapFactory as following:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
if (options.outWidth != -1 && options.outHeight != -1) {
    // This is an image file.
}
else {
    // This is not an image file.
}
zsxwing
  • 20,270
  • 4
  • 37
  • 59
  • 2
    But you would need a `try` `catch` right?.. – IAmGroot Dec 07 '12 at 09:45
  • You can just check the return value of BitmapFactory.decodeFile(path, options); – zsxwing Dec 07 '12 at 09:48
  • 3
    options.inJustDecodeBounds = true decrease the parsing time cost since decodeFile will parse only the width and height of the image. – zsxwing Dec 07 '12 at 09:57
  • @zsxwing if are get system file and witch have no permission to decode then get the null pointer excepetion u can`t read this file and create bitmap.and another thing is when u check the multiple file every time to decode and check it`s increase to generate the outofMemory problem.Just think and then reply. – Zala Janaksinh Dec 07 '12 at 10:29
  • hmm it says false to every image file... – Marco Seiz Dec 07 '12 at 10:30
  • @MarcoSeiz then flag my comment direct the right way to some one. – Zala Janaksinh Dec 07 '12 at 10:57
  • 6
    @zsxwing `decodeFile(String, Options)` will **always** return `null` if you set `inJustDecodeBounds = true`. Use this check to see if it was decoded successfully: `if (options.outWidth != -1 && options.outHeight != -1)` – Baz May 10 '13 at 08:37
  • This is a right way to check whether a file is image.But it's a waste of time. I want another way to check a file effectively. – Allen Vork Jun 13 '16 at 07:45
  • If you can make sure the file extensions are set correctly, checking the file extension name is the most effective way AFAIK. – zsxwing Jun 13 '16 at 18:06
  • "`BitmapFactory.decodeFile()`" great way to kill your app if check includes loads of files – Farid Nov 16 '20 at 07:38
20

Try this code.

public class ImageFileFilter implements FileFilter {
   
    private final String[] okFileExtensions = new String[] {
        "jpg",
        "png",
        "gif",
        "jpeg"
    };


    public boolean accept(File file) {
        for (String extension: okFileExtensions) {
            if (file.getName().toLowerCase().endsWith(extension)) {
                return true;
            }
        }
        return false;
    }

}

It'll work fine.

Use this like new ImageFileFilter(pass file name);

Rohan Pawar
  • 1,875
  • 22
  • 40
Zala Janaksinh
  • 2,929
  • 5
  • 32
  • 58