What is FileFilter?

FileFilter in Android is an interface in Java which can be used to perform operations on specific files. For example, using FileFilter, you can separate different file types, list the number of specific files or check the type of file and such operations.  The instance of FileFilter can be passed to the listFiles(FileFilter) method of File Class. When it is passed as an instance, it filters specific file types.

 

Today we are going to write code that will count the number of audio files present in a specific directory.

Step 1: Add permission to manifest

Read External Storage permission is required to read files from phone storage or SD Card. So, we add it using the code given below.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Step 2: Create a Java class implementing the FileFilter Interface

public class AudioFilter implements FileFilter {

 // only want to see the following audio file types
 private String[] extension = {".aac", ".mp3", ".wav", ".ogg", ".midi", ".3gp", ".mp4", ".m4a", ".amr", ".flac"};

 @Override
 public boolean accept(File pathname) {

 // if we are looking at a directory/file that's not hidden we want to see it so return TRUE
 if (pathname.isDirectory() && !pathname.isHidden()) {
  return true;
 }

 // loops through and determines the extension of all files in the directory
 // returns TRUE to only show the audio files defined in the String[] extension array
 for (String ext : extension) {
   if (pathname.getName().toLowerCase().endsWith(ext)) {
   return true;
  }
 }

 return false;
 }
}

Array named “extension” indicates the type of Files that will be filtered out.

 

Step 3: Create another Java class that uses the AudioFilter class

public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Log.e("Number of Files: ",countNumberOfFiles("directory path"));
 }

 public static int countNumberOfFiles(String path{ 
  File dir = new File(path);
  File[] files = dir.listFiles(new AudioFilter()); 
  int numberOfFiles = files.length; 
  return numberOfFiles; 
 } 
}

On executing the above code, it will log the number of Audio Files present in the directory specified by “directory path”.

Categories: AndroidTutorials

Related Posts

Android

Add Phone Number Authentication in Android (Firebase Tutorial #5)

This tutorial shows how to add Phone Number Authentication in Android app using Firebase. We have used FirebaseUI library for achieving authentication.

Android

Add Shimmer Effect to Android app

Rather than the same old circular loader, you can add shimmer effect to Android app when it is loading. This tutorial shows how you can add shimmer effect to your Android app.

Android

Upload an Image to Firebase Storage (Firebase Tutorial #4)

Today, I am going to show you how to upload an image to Firebase storage and then show that uploaded image in an ImageView in Android.

Subscribe and get a free eBook on 8 Tips to level up your Android Development Skills.

(We hate spam as much as you do)

You have Successfully Subscribed!

%d bloggers like this: