Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Checking if a file size has exceeded a certain value

Tags:

java

android

Is it possible to set a listener on a created file and alert when the file size has reached a certain value.

like image 952
Rashad.Z Avatar asked Nov 10 '22 10:11

Rashad.Z


1 Answers

Yes and no. There is a class called FileObserver which allows you to listen to certain events, for example when the file is opened, closes or modified. There is no specific listener for the file size, but either the MODIFY or the CLOSE_WRITE-event is suitable.

Have a look at the documentation: http://developer.android.com/reference/android/os/FileObserver.html

Short example:

observer = new FileObserver(pathToFile,MODIFY + CLOSE_WRITE)
     @Override
     public void onEvent(int event, String file) {
         Log.d(TAG, "File changed[" + pathToWatch + file + "]");
         //TODO: check file size...
     }
 };
 observer.startWatching();
like image 104
Peanut Avatar answered Nov 14 '22 21:11

Peanut