統計目錄大小和時間刪除早期的大文件 – Android移動開發技術文章_手機開發 Android移動開發教學課程

為什麼要做這些,等你需要的時候你就知道瞭
private void deleteOldestFile(File directory)
{
File[] files = directory.listFiles();
Arrays.sort(files, new Comparator<File>() {
    @Override
    public int compare(File f1, File f2)
    {      
        return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());       
    }});
files[0].delete();
 }
private static long dirSize(File dir) {
long result = 0;
File[] fileList = dir.listFiles();
for(int i = 0; i < fileList.length; i++) {
    if(fileList[i].isDirectory()) {
        result += dirSize(fileList [i]);
    } else {
        // Sum the file size in bytes
        result += fileList[i].length();
    }
 }
return result;
}
 File dirlist = new File(Environment.getExternalStorageDirectory() + "/VideoList");
if(!(dirlist.exists()))
dirlist.mkdir();
Long directorySize = dirSize(dirlist);
 if (directorySize > 1073741824) // this is 1GB in bytes
 {
    deleteOldestFile(dirlist);
 }
File TempFile = new File(Environment.getExternalStorageDirectory()
                + "/VideoList", dateFormat.format(date) + fileFormat);
mediaRecorder.setOutputFile(TempFile.getPath());
或者
while (directorySize > 1073741824)
{
  deleteOldestFile(dirlist);
  direcotrySize = dirSize(dirlist);
}
 

作者“技術人生”

發佈留言