如何将文件保存到一个文件夹,并在应用程序查询它,而不会失去他们的风险,如果他们改名?他们的、并在、如何将、应用程序

2023-09-03 21:50:56 作者:热情狙击手

我的应用程序保存录音上的外部存储。用户输入记录后文件名,然后我再补充一点的文件名ArrayList和使用共享preferences.Now保存ArrayList中,如果用户进入该目录,重命名文件,该文件的名称不会更改共享preferences。这是一个问题,因为当显示在我的应用程序文件列表中,我使用保存在共享preferences的名称打开它。因此,如果一个文件被重命名,这个名字将不会在共享preferences,并会崩溃我的应用程序。反正我有能够避免这种情况,例如 - 我可以保存文件并检索它并不管什么它的名字是打开它?我将如何做呢?

My app saves voice recording on external storage. The user inputs a filename after the recording, and I add that file name to arraylist and save that arraylist using the SharedPreferences.Now, if a user goes to the directory and renames the file, the name of the file will not change in the SharedPreferences. This is a problem because when the list of files is shown in my app, I open it by using the name saved in SharedPreferences. So if a file is renamed, that name will not be in the SharedPreferences, and will crash my app. Is there anyway I can avoid this, for example - can I save a file and retrieve it and open it irrespective of what it's name is? How would I do that?

我因缺乏类似的问题,这让我觉得我失去了保存和检索文件的一个明显的更简单的方法感到惊讶。

I'm surprised by the lack of similar questions, which makes me feel I'm missing an obvious easier way of saving and retrieving files.

推荐答案

这给你一个指定目录中的所有文件的列表:

This gives you a list of all files within a specified directory:

File directory = new File("/path/to/directory/");
File[] files = directory.listFiles();
for( File file : files ) {
    doStuffWithFile( file );
}

没有必要知道文件名,只是该目录。

No need to know the file names, just the directory.