0

I was task to allocate only 1GB of space to store my videos in a particular file directory where it is going to auto-delete the oldest video file in that directory once its about to reach/hit 1GB?

And i eventually found these code but i was left with a problem on how to incorporate these example 1/2 codes into my current existing mainActivity.java file because of the differences in names like "dirlist,tempFile" compared with other examples 1/2 given to perform the task of size checking and deleting.

Sorry i'm kinna new in android/java therefore i don't really know what "fields" to change to suit my current coding needs? Can someone help on how am i going to complie these set of codes into a single set of code which perform the above mention functions??

My Current existing mainActivity.java

File dirlist = new File(Environment.getExternalStorageDirectory() + "/VideoList");

if(!(dirlist.exists()))
    dirlist.mkdir();

File TempFile = new File(Environment.getExternalStorageDirectory() 
                    + "/VideoList", dateFormat.format(date) + fileFormat);
mediaRecorder.setOutputFile(TempFile.getPath());

(Example 1) code for summing up directory file size in a given folder..

private static long dirSize(File dir) {
    long result = 0;

    Stack<File> dirlist= new Stack<File>();
    dirlist.clear();

    dirlist.push(dir);

    while(!dirlist.isEmpty())
    {
        File dirCurrent = dirlist.pop();

        File[] fileList = dirCurrent.listFiles();
        for (int i = 0; i < fileList.length; i++) {

            if(fileList[i].isDirectory())
                dirlist.push(fileList[i]);
            else
                result += fileList[i].length();
        }
    }

    return result;
}

(Example 2) set of code for getting all the files in an array, and sorts them depending on their modified/created date. Then the first file in your array is your oldest file and delete it.

//   no idea what are the parameters i should enter 
//   here for my case in mainActivity?? 
File directory = new File((**String for absolute path to 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());        
        }});

file[0].delete();
Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
zack
  • 37
  • 8
  • 1
    also don't post essentially the same question twice. and please show what you have tried, what errors you're getting, etc... there is nothing particular about these code snippets that would make them hard to integrate anywhere. – Mat May 01 '11 at 09:09
  • @Mat the problem is i am hardly only have 6 weeks of programming experience so far in android/java which make me a newbie with no good idea on what i should change above to suit my codes... Haiz :( – zack May 01 '11 at 10:42

1 Answers1

1

This is a reference to your previous question: How do I put a capped maximum directory storage space in SD?. In the future, you should keep discussions about the same topic in the same question, rather than create 2 new identical questions.

In your Activity class lets say you define these 2 methods:

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; 
}

You can now do this with your code:

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());

So before setting the output file in that folder, it checks if the folder is > 1GB, and if so, deletes the oldest file first.

To be honest though, deleting the oldest file may not necessarily make the directory size < 1GB, so i would use a while loop to ensure that it is < 1GB like so:

while (directorySize > 1073741824)
{
  deleteOldestFile(dirlist);
  direcotrySize = dirSize(dirlist);
}
Community
  • 1
  • 1
Hakan Ozbay
  • 4,639
  • 2
  • 22
  • 28
  • Hey your solution works fine :) but i have a small problem here, why is the statement "dirlist.length" [for(int i = 0; i < dirlist.length; i++) ] with error saying "length cannot be resolved or is not a field" Should i change it to length() to solve the problem? Thanks – zack May 01 '11 at 13:58
  • Apologies that should be "filelist.length" ive corrected it in in the answer. – Hakan Ozbay May 01 '11 at 14:04
  • Thanks You very much really :) But sorry to trouble you again could you visit this link --> to sovle another problem of mine http://stackoverflow.com/questions/5791901/how-to-use-timer-to-restart-record-video-for-every-preset-interval-time Because i've also wanted also "dyanmic" interval recording from preference and problem intergrating it into codes too... – zack May 01 '11 at 14:47