为什么 DocumentFile.getParentFile() return 在使用 Uri 创建后为空?

Why does DocumentFile.getParentFile() return null after it is created with a Uri?

代码:

DocumentFile dfNew = dfDirectory.createFile("video/mp4", "foo.mp4");
//dfNew.getParentFile() is NOT null.  
DocumentFile dfNewCopy = DocumentFile.fromSingleUri(activity, dfNew.getUri());
//dfNewCopy.getParentFile() IS null. Otherwise, dfNewCopy works fine

为什么从 DocumentFile 的 Uri 创建的 DocumentFile 在这方面与原始文件不同?

存储访问框架是在 API 16 天为非常狭窄的用例设计的(实现第一个官方 Android 文件选择器)。 DocumentsProvider 当时缺乏很多有用的功能。平台后续版本新增更多方法

检查一个 Uri 是否是另一个 Uri (isChildDocument) 的父项所需的方法已添加到 API 21.

在API 26 (Oreo) 中添加了获取 Uri 父项 (findDocumentPath) 的方法。

DocumentFile 不能return旧平台上的父 Uri,因为系统本身不提供它。