Breaking News
Home / Android / Android Snippets / How to check if a file is an Image in Android using FileFilters?

How to check if a file is an Image in Android using FileFilters?

In order to check if file is an Image, one can check for the file extension (jpg, png, etc.) using FileFilter as shown below:

  1. package com.livrona.apps.sikh.wallpapers;
  2.  
  3. import java.io.File;
  4. import java.io.FileFilter;
  5.  
  6.  
  7. // TODO: Auto-generated Javadoc
  8. /**
  9.  * The Class CheckImageExtension.
  10.  */
  11. public class CheckImageExtension {
  12.  
  13.  
  14.      /** The is valid image. */
  15.      public static FileFilter isValidImage = new FileFilter() {
  16.  
  17.  
  18.           @Override
  19.           public boolean accept(File pathname) {
  20.                final String name = pathname.getName();
  21.                String ext = null;
  22.                int i = name.lastIndexOf(‘.’);
  23.  
  24.  
  25.                if (i > 0 && i < name.length() 1) {
  26.                     ext = name.substring(i + 1).toLowerCase();
  27.                }
  28.                if (ext == null)
  29.                     return false;
  30.                else if (!ext.equals("jpg") && !ext.equals("jpeg") && !ext.equals("png") && !ext.equals("gif"))
  31.                     return false;
  32.                else
  33.                     return true;
  34.           }
  35.      };
  36.  
  37. }
[pb_builder]

Check Also

How to check if an Android device can connect to Internet?

The following code snippet shows how to check if an Android device can connect to …

Advertisment ad adsense adlogger