Tuesday, July 7, 2009

Detect when an image is added or removed in the BlackBerry device file system

import net.rim.device.api.io.file.FileSystemJournal;
import net.rim.device.api.io.file.FileSystemJournalEntry;
import net.rim.device.api.io.file.FileSystemJournalListener;
import net.rim.device.api.ui.component.Dialog;

public class FileExplorerDemoJournalListener implements FileSystemJournalListener  {

private long _lastUSN;
private String path;
public void fileJournalChanged() {
long nextUSN = FileSystemJournal.getNextUSN();
for (long lookUSN = nextUSN - 1; lookUSN >= _lastUSN; --lookUSN) {
FileSystemJournalEntry entry = FileSystemJournal.getEntry(lookUSN);
if (entry == null) {
   break; 
}
path = entry.getPath();
switch (entry.getEvent()) {
   case FileSystemJournalEntry.FILE_ADDED:
        Dialog.alert(path);
       break;
   case FileSystemJournalEntry.FILE_DELETED:
         Dialog.alert(path);
       break;
       }
}
}
}

---------------------- Main-----------------------------------
import net.rim.device.api.ui.UiApplication;

public class Main extends UiApplication {

public static void main(String[] args) {
Main theMain = new Main();
theMain.enterEventDispatcher();
}
public Main() {
this.addFileSystemJournalListener(new FileExplorerDemoJournalListener());
}

Post a Comment