如何将下面的这些代码合并到我当前的现有代码中?

发布于 2024-11-04 08:19:30 字数 1833 浏览 0 评论 0原文

我的任务是只分配 1GB 的空间来将我的视频存储在特定的文件目录中,一旦该目录即将达到/达到 1GB,它将自动删除该目录中最旧的视频文件?

我最终找到了这些代码,但我留下了一个问题,如何将这些示例 1/2 代码合并到我当前现有的 mainActivity.java 文件中,因为与其他示例 1/2 相比,“dirlist,tempFile”等名称存在差异用于执行大小检查和删除的任务。

抱歉,我是 android/java 新手,因此我真的不知道要更改哪些“字段”来满足我当前的编码需求?有人可以帮助我如何将这些代码集编译成执行上述功能的一组代码吗?

我当前现有的 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());

(示例 1)代码用于总结给定文件夹中的目录文件大小。

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

(示例 2)代码集用于获取数组中的所有文件,并根据其修改/创建日期对它们进行排序。然后数组中的第一个文件是最旧的文件并将其删除。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听,心雨的声音 2024-11-11 08:19:31

这是对您之前问题的引用: 如何在 SD 中设置最大目录存储空间上限?。以后,您应该在同一问题中继续讨论同一主题,而不是创建 2 个新的相同问题。

在您的 Activity 类中,假设您定义了这两种方法:

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

因此,在该文件夹中设置输出文件之前,它会检查该文件夹是否> >。 1GB,如果是,则先删除最旧的文件。

但说实话,删除最旧的文件可能不一定使目录大小 1GB,所以我会使用 while 循环来确保它 < 1GB 像这样:

while (directorySize > 1073741824)
{
  deleteOldestFile(dirlist);
  direcotrySize = dirSize(dirlist);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文