Delete Files from directory based on directory size.

Tutorials with advanced 'difficulty' and more Lines of Code.

Delete Files from directory based on directory size.

Postby Dannible » Fri May 14, 2010 4:46 pm

In my application I have a directory where I keep images temporarily. I would like to be able to delete the images from this directory when it reaches a certain size. I don't need it to delete the oldest first or anything like that. I just need to keep the size in check.


This is what I have:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void DelSomeCache(Long dirSize){
  2.  
  3.                 long dirFileSize = 0;
  4.  
  5.                
  6.  
  7.                 String sDir = Environment.getExternalStorageDirectory()+"/myDir";
  8.  
  9.                 File delDir = new File(sDir);  
  10.  
  11.                 String[] files = delDir.list();
  12.  
  13.        
  14.  
  15.                 for(int i=0;i<files.length;i++){
  16.  
  17.                         File ftdel = new File(files[i]);
  18.  
  19.                         long fileSize = ftdel.length();
  20.  
  21.                        
  22.  
  23.                         dirFileSize = dirFileSize + fileSize;
  24.  
  25.                        
  26.  
  27.                         if(dirFileSize>=dirSize){
  28.  
  29.                                 Log.i("Deleted",files[i]);
  30.  
  31.                                 ftdel.delete();
  32.  
  33.                         }
  34.  
  35.                 }
  36.  
  37.                
  38.  
  39.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


It does not seem to be working. I get a message in the LogCat that files were deleted, but when I check the files are still there. Any help with what i'm missing would be appreciated.
Dannible
Junior Developer
Junior Developer
 
Posts: 13
Joined: Fri Nov 13, 2009 11:43 pm

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests