FileObserver仅捕获删除事件

时间:2018-05-28 12:43:39

标签: android service fileobserver

我有一个服务,它启动一个FileObserver来监控手机摄像头拍摄的照片,特别是文件:

String initialPictureFolder =  Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString() + "/Camera/"; 

通过这个功能:

public void startFileObserver(String filePath){
    /* It only monitors this Folder, not any Subfolder */
    myFileOsberver = new PicturesFileObserver(filePath,UploadPhotosService.this);
    myFileOsberver.startWatching();
}

说,FileObserver只捕获删除事件(代码512),仅此而已。每当我重命名文件,移动文件或从Android摄像头拍照时,都没有任何反应。我错过了什么吗?我监控错误的文件吗?

这是整个FileObserver类

private static int mask = FileObserver.ALL_EVENTS; 

public PicturesFileObserver(String path,UploadPhotosService myService) {
    super(path,mask);
    this.rootDir = path;
    this.myService = myService;
    Log.e(TAG,"Observer is initialized on: "+rootDir);

}

@Override
public void onEvent(int event, @Nullable String path) {
   event &= FileObserver.ALL_EVENTS;
   Log.e(TAG,String.valueOf(event)); // ONLY PRINTS 512 
    if (path != null) {
        if (path.endsWith(".jpg") || path.endsWith(".png")) {
            if ((event == FileObserver.CREATE)) {
                Log.e(TAG, "File was created " + path);

            } else if ((FileObserver.MODIFY & event) != 0) {
                Log.e(TAG, "File was modified " + path);
            } else if ((FileObserver.DELETE & event) != 0) {
                Log.e(TAG, "File was deleted " + path);
            } else if ((FileObserver.MOVED_FROM & event) != 0) {
                Log.e(TAG, "File was brought here " + path);
            } else if ((FileObserver.MOVED_TO & event) != 0) {
                Log.e(TAG, "File was brought here " + path);
            }
        } else
            Log.e(TAG, "Path not beeing a photo: " + path);
    }
}

0 个答案:

没有答案