0

Even if I deleted 50% of files from a directory ,directory size remains same.I want to reduce directory size also.Here is my code:

    Log.d("rohith", "Before Clean cacheDir from File Cache "+cacheDir.length());
    File[] files=cacheDir.listFiles();
    Log.d("rohith", "files.length="+files.length);

        for(int i=files.length;i>50;i--)
        {
            Log.d("rohith2", "files[i].getName()= "+files[i-1].getName());
            File f = new File(cacheDir,files[i-1].getName());
            boolean result = f.delete();

        }
        files=cacheDir.listFiles();
        Log.d("rohith", "files.length="+files.length);
        Log.d("rohith", "after Clean cacheDir from File Cache "+cacheDir.length());
Rohith R Nair
  • 23
  • 1
  • 4

1 Answers1

0

with cacheDir.lenght(), you don't get the actual folder size (with all files in it). Look at this question to get the actual folder size:

How can I get the size of a folder on SD card in Android?

Community
  • 1
  • 1
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
  • But after deleting all the files in the directory,then deleting the folder by : cacheDir.delete() give 0 size for cacheDir.length() – Rohith R Nair Oct 29 '13 at 10:26
  • I looked it up, it's the binary lenght of the folder. Only the folder, without the files inside. Usually, the size is around 2-4 KB – Manuel Allenspach Oct 29 '13 at 10:44