DocumentFile
Also known as: document file android, SAF file wrapper, android documentfile
DocumentFile is an AndroidX helper class that wraps a Storage Access Framework document tree URI in a File-like API, letting apps list, create, rename, and delete files under a user-granted folder without raw filesystem paths.
- Lives in the androidx.documentfile (DocumentFile) support library, not the core SDK.
- Created from a tree URI with DocumentFile.fromTreeUri after ACTION_OPEN_DOCUMENT_TREE.
- Wraps SAF documents in File-like calls: listFiles, length, delete, createFile.
What DocumentFile is
DocumentFile is a convenience class in the androidx.documentfile library that gives Storage Access Framework (SAF) URIs an interface resembling the legacy java.io.File API. Because scoped storage blocks direct path access to most external storage on Android 10+, DocumentFile is the practical way to traverse a folder the user picked with ACTION_OPEN_DOCUMENT_TREE.
You typically start from DocumentFile.fromTreeUri(context, treeUri) after taking a persistable permission, then call listFiles() to enumerate children and getName(), length(), lastModified(), and isDirectory() to inspect each entry. Creation and removal go through createFile(), createDirectory(), and delete().
How cleaners use it
After the user grants a tree URI, a cleaner persists access with takePersistableUriPermission() so the grant survives reboots, then walks the tree with DocumentFile to find large files, duplicates, or stale downloads. Each candidate is removed by calling delete() on its DocumentFile, which the framework routes through the owning DocumentsProvider.
DocumentFile is convenient but slower than raw paths because every operation crosses an IPC boundary into the provider. For listing large directories, apps often query the DocumentsContract child-documents URI directly via ContentResolver for speed and use DocumentFile mainly for individual file actions like delete or rename.