由于 DocumentFile().fromTreeUri(),尚不存在的路径上的 DocumentFile.exists() 始终返回 true

Dun*_*dle 5 java android uri documentfile

我正在尝试使用 DocumentFile(由于存储访问框架)在创建文件之前检查文件是否存在。但是DocumentFile().fromTreeUri()删除了可能的 Uri 的不存在部分,这会导致DocumentFile().exists()始终返回 true,无论它是否存在。

我创建了一个简单的例子来证明我的观点。首先我们要求用户选择一个目录:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    // Ask the user for the source folder
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    startActivityForResult(intent, 100);
}
Run Code Online (Sandbox Code Playgroud)

在响应时,我们添加/fictionalFile到路径(从而使其成为一个不存在的文件),然后检查它是否存在:

public void onActivityResult(int requestCode, int resultCode, Intent resultData)
{
    if (resultCode == RESULT_OK)
    {
        if(requestCode == 100)
        {
            Uri fictionalURI = Uri.parse(resultData.getData()+"/fictionalFile");
            DocumentFile fictionalFile = DocumentFile.fromTreeUri(this, fictionalURI);
            Log.i("STORAGE", "FICTIONAL URI: "+fictionalURI);
            Log.i("STORAGE", "FICTIONAL DOCUMENTFILE URI: "+fictionalFile.getUri());

            if(fictionalFile.exists())
            {
                Log.i("STORAGE", "Fictional file exists");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当 DocumentFile.fromTreeUri() 在虚构的 Uri 上运行时,伪造的“/fictionalfile”部分丢失了,这会导致 DocumentFile.exists() 函数返回 true,如下面的 LogCat 所示:

I/STORAGE: FICTIONAL URI: content://com.android.externalstorage.documents/tree/17FA-1C18%3AFileSync%2Ftarget/fictionalFile

I/STORAGE: FICTIONAL DOCUMENTFILE URI: content://com.android.externalstorage.documents/tree/17FA-1C18%3AFileSync%2Ftarget/document/17FA-1C18%3AFileSync%2Ftarget

I/STORAGE:存在虚构文件

(在上面的例子中,我使用的是 SD 卡,因此路径名很长)

是否有另一种方法来检查尚未创建的 DocumentFile 是否存在?用例是将文件从目录 A 复制到目录 B 时,我想在开始传输之前检查目录 B 中是否已存在所述文件。

更新:我现在意识到使用DocumentFile.fromTreeUri()是错误的,我应该使用DocumentFile.fromSingleUri(). 这有帮助,但是在运行.exists()新文件时,我得到了W/DocumentFile: Failed query: java.lang.UnsupportedOperationException: Unsupported Uri content://com.android.externalstorage.documents/tree/17FA-1C18%3AFileSync%2Ftarget/fictionalFile. 有什么想法吗?

public void onActivityResult(int requestCode, int resultCode, Intent resultData)
{
    if (resultCode == RESULT_OK)
    {
        if(requestCode == 100)
        {
            Uri fictionalURI = Uri.parse(resultData.getData()+"/fictionalFile");
            DocumentFile fictionalFile = DocumentFile.fromSingleUri(this, fictionalURI);
            Log.i("STORAGE", "FICTIONAL URI: "+fictionalURI);
            Log.i("STORAGE", "FICTIONAL DOCUMENTFILE URI: "+fictionalFile.getUri());

            if(fictionalFile != null && fictionalFile.exists())
            {
                Log.i("STORAGE", "Fictional file exists");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Com*_*are 5

鉴于treeUri作为是Uri由归国ACTION_OPEN_DOCUMENT_TREE,包裹treeUriDocumentFile使用fromTreeUri(),然后调用findFile()DocumentFile提供的显示名称,你正在寻找(如fictionalFile)。如果返回null,则没有与该显示名称匹配的文件。

爱荷华州:

if (DocumentFile.fromTreeUri(this, treeUri).findFile(whatevs) == null) {
  // TODO: something
}
Run Code Online (Sandbox Code Playgroud)

但请注意,“显示名称”不一定是文件名。