相关疑难解决方法(0)

SAF DocumentFile-检查路径是否存在,而不在每个文件夹级别创建每个DocumentFile

成像,您要检查是否存在“ /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)

android storage-access-framework documentfile

3
推荐指数
1
解决办法
1354
查看次数