简体   繁体   中英

I want to delete folder if the size is above 10MB [on hold]

I have a folder in my sdcard named Drm, so i just want to make a program which can be active everytime in my phone, and it'll just scan the folder. When the folder's data size is more than 10MB, it'll delete all the datas inside it.

Using Java in Android.

I'd make a method to calculate a folder size and then, if the size exeeds 10M - remove the folder:

private long getFolderSize(File folder) {
    long length = 0;
    File[] files = folder.listFiles();

    int count = files.length;

    for (int i = 0; i < count; i++) {
        if (files[i].isFile()) {
            length += files[i].length();
        }
        else {
            length += getFolderSize(files[i]);
        }
    }
    return length;
}

File folder = new File ...
if (getFolderSize(folder) > 1024 * 1024 * 10)) {
    FileUtils.deleteDirectory(dir); // Apache IO
}

And then this app should be create as a deamon/service in order to keep it running The code can be called either by Android's class Timer or create ScheduledExecutorService

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM