What is FileObserver?

A FileObserver in Android is an interface that monitors file access and modification. It triggers an event any time a file is accessed or changed by any process on the device.


Each FileObserver instance monitors either a file or a directory. And if the instance is observing a directory, events will be triggered for all files and sub-directories inside the monitored directory.


If a FileObserver is garbage collected, it will stop sending events. Also, FileObserver must be referenced from some live project to ensure that it works properly.

Geeky Stuff

  • FileObserver is an abstract class. Consequently, one must create a subclass that extends the FileObserver class.
  • The subclass also must implement the OnEvent method.
  • FileObserver is based on Linux’s iNotify. iNotify is a simple but powerful file change notification system built into the Linux 2.6.13 kernel and above.

The Code

Today, we will be creating a FileObserver that will observe a particular directory aka Directory FileObserver. And this FileObserver will fire an event when any file is created in the Observing Directory.

Step 1: Add Read External Storage Permission to Manifest File

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

Step 2: Create a subclass that extends FileObserver.

public class DirectoryFileObserver extends FileObserver {

String aboslutePath = "path to your directory";

public DirectoryFileObserver(String path) {
aboslutePath = path;

public void onEvent(int event, String path) {
if(path != null){
Log.e("FileObserver: ","File Created");

Step 3: Call the FileObserver from Activity

public class MainActivity extends AppCompatActivity {

DirectoryFileObserver directoryFileObserver;

protected void onCreate(Bundle savedInstanceState) {
context = this;
directoryFileObserver = new DirectoryFileObserver(Path of the directory);

When you execute the above code, it will display a log message “File Created”, every time a new File or Directory is created in the directory referred by the variable absolutePath.

FileObserver in Android supports many types of events. For the complete list click here.

Categories: AndroidTutorials

  • daniele

    Hi, How I can keep working fileobserver in a service (background)?

    • Starting from Android Oreo, you can’t run a service for long period of time. So, you should consider using Job Scheduler or Firebase Job Dispatcher for running your file observer code at regular intervals.

      • daniele

        I’m working with Marsmallow marshmallow

        • But still Android Oreo users will be using your app and you don’t want to crash your app on their phones. And Firebase Job Dispatcher and Job Scheduler do work on Marshmallow.

          • Hello, I’m making widget showing recent taken photos, it refresh itself every 30min but I want to trigger update by file observer when new photo is shown in selected directory, I don’t know how to get it working if not in service 🙁

%d bloggers like this: