Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to check if file is image? [duplicate]

Tags:

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!

like image 551
Marco Seiz Avatar asked Dec 07 '12 09:12

Marco Seiz


2 Answers

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. } 
like image 114
zsxwing Avatar answered Oct 18 '22 16:10

zsxwing


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

like image 34
Zala Janaksinh Avatar answered Oct 18 '22 16:10

Zala Janaksinh