0

I am trying to monitor all files operations(create, read, write, modify, delete) in ‘/sdcard’ directory. It seems the class FileObserver could do the job from the documentation”https://developer.android.com/reference/android/os/FileObserver.html”. Iimplement onEvent method like this,

 @Override
    public void onEvent(int event, @Nullable String path) {
        Log.d("sdevent",""+event);
        switch(event) {
            case FileObserver.ALL_EVENTS:
                Log.d("sdcardlistener", "path:"+ path);
                break;
            case FileObserver.CREATE:
                Log.d("sdcardlistener", "path:"+ path);
                break;
        }
    }

But when I try it on my Android studio 3.5.3 using emulator Pixel2(API 28) and my own phone which is Android Q now, I cannot get any event on path ‘/sdcard’ or its subdirectory ,‘/sdcard/DCIM’ for example. The code is like this.

SDCardListener listener1=new SDCardListener("/sdcard");
listener1.startWatching();

If I change the path to a app-specific one, like ‘/sdcard/Android/data/com.example.myfiletest(my app name)/files/apple’, it seems to work for the directory and its fist subdirectory.

SDCardListener listener2=new SDCardListener("/sdcard/Android/data/com.example.myfiletest/files/apple");
listener2.startWatching();

Since this situation doesn’t match documentation, I want to know where is the problem. And is there a way to monitor all file changes in externel storage if FileObserver not capable to do so.

JJ Meng
  • 11
  • 2

0 Answers0