Android: How to check if file is image?

28,587

Solution 1

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);

Solution 2

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.
}
Share:
28,587

Related videos on Youtube

Marco Seiz
Author by

Marco Seiz

Android (Newbie) Developer

Updated on October 06, 2020

Comments

  • Marco Seiz
    Marco Seiz over 3 years

    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!

    • Praful Bhatnagar
      Praful Bhatnagar over 11 years
      may be you can check with the file extension..
    • My Head Hurts
      My Head Hurts over 11 years
    • Marco Seiz
      Marco Seiz over 11 years
      I think it's not the right way to check with file extension. Because I would have to create a function that checks EVERY image file extensions like bmp, jpg, jpeg, png, gif, tif, etc...
  • IAmGroot
    IAmGroot over 11 years
    But you would need a try catch right?..
  • zsxwing
    zsxwing over 11 years
    You can just check the return value of BitmapFactory.decodeFile(path, options);
  • zsxwing
    zsxwing over 11 years
    options.inJustDecodeBounds = true decrease the parsing time cost since decodeFile will parse only the width and height of the image.
  • Zala Janaksinh
    Zala Janaksinh over 11 years
    @zsxwing if are get system file and witch have no permission to decode then get the null pointer excepetion u cant read this file and create bitmap.and another thing is when u check the multiple file every time to decode and check its increase to generate the outofMemory problem.Just think and then reply.
  • Marco Seiz
    Marco Seiz over 11 years
    hmm it says false to every image file...
  • Marco Seiz
    Marco Seiz over 11 years
    That worked... Now I have to search every image extension...
  • Zala Janaksinh
    Zala Janaksinh over 11 years
    @MarcoSeiz then flag my comment direct the right way to some one.
  • Baz
    Baz almost 11 years
    @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
    Baz almost 11 years
    @ZalaJanaksinh This would return true for any file that has an "image file extension", e.g. if I rename test.mp3 to test.jpg, your method would return true.
  • Allen Vork
    Allen Vork about 8 years
    it's not the right way. Because not all the path includes the image type is a image
  • Zala Janaksinh
    Zala Janaksinh about 8 years
    @AllenVork then suggest what is the right way? && what is the drawback of use of that ?
  • Allen Vork
    Allen Vork about 8 years
    try the solution below
  • Allen Vork
    Allen Vork almost 8 years
    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.
  • zsxwing
    zsxwing almost 8 years
    If you can make sure the file extensions are set correctly, checking the file extension name is the most effective way AFAIK.
  • Reaz Murshed
    Reaz Murshed over 7 years
    zsxwing's answer works perfectly.
  • Farid
    Farid over 3 years
    "BitmapFactory.decodeFile()" great way to kill your app if check includes loads of files