How Do I Put A Capped Maximum Directory Storage Space In Sd?
Presumingly i wanted to allocate only 1GB of space to store my videos in a particular file directory where how is it going to auto-delete the oldest video file in that directory on
Solution 1:
To get the current size of a directory, you need add up the sizes of each individual file in a directory using the length()
method. This article is what you're looking for in that respect. You can then check if the size has exceeded 1 GB.
In terms of auto-deleting the oldest file you can do the following:
File directory = new File(*Stringfor absolute path to directory*);
File[] files = directory.listFiles();
Arrays.sort(files, new Comparator<File>() {
@Override
publicint compare(File f1, File f2) {
return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
}
});
file[0].delete();
This code gets all your files in an array, and sorts them depending on their modified/created date. Then the first file in your array is your oldest file, therefore you can just simply delete it.
The best place to put this in your code is when you're about to write something to a directory. Perform this check and deletion first, and then if the size is less than 1 GB, write to directory, otherwise delete another file.
Post a Comment for "How Do I Put A Capped Maximum Directory Storage Space In Sd?"