Delphi.Boy
Delphi.Boy

Reputation: 1216

Monitoring File Access on Android

I need to monitor files that are accessed (or tried to access and failed) by a specific program on my Android device. Something like FileMon on PCs.

There are suggestions on the Net to decompile the program, but I'm looking for a more straight way.

Thanks.

Upvotes: 3

Views: 3134

Answers (1)

Robin
Robin

Reputation: 10368

You should use standard android.os.FileObserver, this Monitors files (using inotify) to fire an event after files are accessed or changed by by any process on the device (including this one). FileObserver is an abstract class; subclasses must implement the event handler onEvent(int, String). Refer to this link http://developer.android.com/reference/android/os/FileObserver.html

Simple example here:

import android.os.FileObserver;   
import android.util.Log;   


    /**  
     * SD Card file monitor  
     * 
     */  
public class SDCardListener extends FileObserver {   

       public SDCardListener(String path) { 
              super(path);   
       }   

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

SDCardListener listener = new SDCardListener("YOURDIRPATH");   

//Start monitoring   
listener.startWatching();

// You can try to access the directory here 

//Stop monitoring   
listener.stopWatching();  

One thing you should bare in mind is that you should keep a valid reference to the file observer object to prevent it from GC.

Upvotes: 5

Related Questions