Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SAF DocumentFile-检查路径是否存在,而不在每个文件夹级别创建每个DocumentFile_Android_Storage Access Framework_Documentfile - Fatal编程技术网

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

Android SAF DocumentFile-检查路径是否存在,而不在每个文件夹级别创建每个DocumentFile,android,storage-access-framework,documentfile,Android,Storage Access Framework,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

如果您想检查“/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 (doc == null)
{
    // file does not exist
}
else
{
    // file does exist
}
DocumentFile sdCard=…;//我已经通过SAF在用户帮助下检索了sd卡根目录
字符串路径=“/folder/subfolder/subsubfolder/test/test.txt”;
List pathParts=Arrays.asList(path.split(“/”);
DocumentFile doc=sdCard;
//检查所有文件夹,从sd卡开始,检查所需文件是否存在
对于(int i=1;i
这是非常缓慢的,有没有更快的方法至少可以检查sd卡上是否存在文件?我不想创建每个
文档文件
只是为了检查路径是否存在

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri uri = data.getData();
    DocumentFile pickedDir = DocumentFile.fromTreeUri(MainActivity.this, uri);
    String id = DocumentsContract.getTreeDocumentId(uri); 

    /* id is :  "primary:namefolder" if user select a "namefolder" from internal storage. 
     or CABB-5641 if user select external storage,
    */

    id = id + "/folder/subfolder/subsubfolder/test/test.txt";  // your path,  you must to ensure is consistent with that chosen by the user,

    Uri childrenUri = DocumentsContract.buildDocumentUriUsingTree(uri,id);
    DocumentFile chhildfile = DocumentFile.fromSingleUri(MainActivity.this,childrenUri);

    if(chhildfile != null && chhildfile.exists()){
        Log.d("laporan file", "file ini ada");
    }
}

我不是专家,我不知道这是否适用于大多数android,但我已经在android emulator和华硕Zenfone 5中进行了尝试,希望这将有助于

如果您有解决方案的话?没有比我问题中发布的更好的解决方案了……谢谢!我在这上面花了8个小时!SAF真是一团糟!