为什么DocumentFile.getParentFile()在用Uri创建后返回null?

时间:2018-02-22 00:49:01

标签: storage-access-framework android-storage documentfile

代码:

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与此方面的原始文件不同?

2 个答案:

答案 0 :(得分:2)

存储访问框架已经在API 16的几天内设计用于非常狭窄的用例(实现第一个官方Android文件选择器)。 DocumentsProvider当时缺乏很多有用的功能。在以下版本的平台中添加了更多方法。

在API 21中添加了检查一个Uri是否是另一个Uri(isChildDocument)的父级所需的方法。

在API 26(奥利奥)中添加了获取Uri的父级(findDocumentPath)的方法。

DocumentFile无法在较旧的平台上返回父Uri,因为系统本身并不提供它。

答案 1 :(得分:0)

我通过以下替代功能解决了这个问题:

    {
"thingsList": [
        {
            "id": 0,
            "name":"Orange",
            "from": "Jill",
            "fromImage": "images/IMG_9352.jpg",
            "createDate": "2018-8-817:57:28.556094Z",
            "needBy": "Today",
            "description":"to put for kids box tomo, bring from down stores",
            "status": "open",
            "statusUpdatedOn": "2018-8-8T17:57:28.556094Z",
            "statusUpdatedBy": "Jack",
            "assignedTo": "Jack"
        },
        {
            "id": 1,
            "name":"Cryons",
            "from": "Jill",
            "fromImage": "images/IMG_9352.jpg",
            "createDate": "2018-8-8T17:57:28.556094Z",
            "needBy": "In a week",
            "description":"bring from opp bangle store",
            "status": "open",
            "statusUpdatedOn": "",
            "statusUpdatedBy": "Jack",
            "assignedTo": "Jack"
        },
        {
            "id": 2,
            "name":"Eggs",
            "from": "Jill",
            "fromImage": "images/IMG_9352.jpg",
            "createDate": "2018-9-8T17:57:28.556094Z",
            "needBy": "Today",
            "description": "some 10 eggs",
            "status": "open",
            "statusUpdatedOn": "2018-8-10T17:57:28.556094Z",
            "statusUpdatedBy": "Jack",
            "assignedTo": "Jack"
        },
        {
            "id": 3,
            "name": "KG sheet",
            "from": "Jill",
            "createDate": "2018-8-6T17:57:28.556094Z",
            "fromImage": "images/IMG_9352.jpg",
            "needBy": "Today",
            "description":"black n white, 1 each",
            "status": "open",
            "statusUpdatedOn": "2018-8-9=T17:57:28.556094Z",
            "statusUpdatedBy": "Jack",
            "assignedTo": "Jack"

        }
    ]
}