如何将下面的这些代码合并到我当前的现有代码中?
我的任务是只分配 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是对您之前问题的引用: 如何在 SD 中设置最大目录存储空间上限?。以后,您应该在同一问题中继续讨论同一主题,而不是创建 2 个新的相同问题。
在您的 Activity 类中,假设您定义了这两种方法:
您现在可以使用代码执行此操作:
因此,在该文件夹中设置输出文件之前,它会检查该文件夹是否> >。 1GB,如果是,则先删除最旧的文件。
但说实话,删除最旧的文件可能不一定使目录大小 1GB,所以我会使用 while 循环来确保它 < 1GB 像这样:
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:
You can now do this with your code:
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: