Thursday, 15 April 2010

android - FileObserver -> onEvent(event, path): path is NULL -



android - FileObserver -> onEvent(event, path): path is NULL -

i want know when file finished writing, , i'm trying utilize fileobserver. i'm doing this:

fileobserver observer = new fileobserver(imageuri.getpath()) { @override public void onevent(int event, string path) { if(event == fileobserver.close_write) log.d(tag, "file: "+path); } }; observer.startwatching();

imageuri valid uri. when file closed next log entry:

file: null

why null? it's possible user writes several files, need know 1 triggering event.

thanks!

according documentation of onevent():

the path, relative main monitored file or directory, of file or directory triggered event

so guess when path null the specified file or directory...

you need maintain track of original path yourself. , append path of onevent() path total path (unless tracking file , value null):

fileobserver observer = new fileobserver(imageuri.getpath()) { public string basepath; @override public void onevent(int event, string path) { string fullpath = basepath; if(path != null) { // add together '/' in between (depending on basepath) fullpath += path; } log.d(tag, "file: "+fullpath); } }; observer.basepath = imageuri.getpath(); observer.startwatching();

i tried maintain illustration close code snippet possible. but, much improve create full-blown class extending fileobserver, can add together constructor store basepath , not required access public field outside class/instance!

android fileobserver

No comments:

Post a Comment