成像,您要检查是否存在“ /folder/subfolder/subsubfolder/test/test.txt”文件,请执行以下操作:
DocumentFile sdCard = ...; // i have already retrieved the sd card root with the users help via SAF
String path = "<SD CARD>/folder/subfolder/subsubfolder/test/test.txt";
List<String> pathParts = Arrays.asList(path.split("/"));
DocumentFile doc = sdCard;
// go through all folders, starting at sd card, to check, if the desired file exists
for (int i = 1; i < pathParts.size(); i++)
{
DocumentFile nextDoc = doc.findFile(pathParts.get(i));
if (nextDoc != null)
doc = nextDoc;
else
{
doc = null;
break;
}
}
if …Run Code Online (Sandbox Code Playgroud)