O Google tem o compromisso de promover a igualdade racial para as comunidades negras. Saiba como.

DocumentFile

abstract class DocumentFile
kotlin.Any
   ↳ androidx.documentfile.provider.DocumentFile

Representation of a document backed by either a android.provider.DocumentsProvider or a raw file on disk. This is a utility class designed to emulate the traditional File interface. It offers a simplified view of a tree of documents, but it has substantial overhead. For optimal performance and a richer feature set, use the android.provider.DocumentsContract methods and constants directly.

There are several differences between documents and traditional files:

  • Documents express their display name and MIME type as separate fields, instead of relying on file extensions. Some documents providers may still choose to append extensions to their display names, but that's an implementation detail.
  • A single document may appear as the child of multiple directories, so it doesn't inherently know who its parent is. That is, documents don't have a strong notion of path. You can easily traverse a tree of documents from parent to child, but not from child to parent.
  • Each document has a unique identifier within that provider. This identifier is an opaque implementation detail of the provider, and as such it must not be parsed.

Before using this class, first consider if you really need access to an entire subtree of documents. The principle of least privilege dictates that you should only ask for access to documents you really need. If you only need the user to pick a single file, use Intent#ACTION_OPEN_DOCUMENT or Intent#ACTION_GET_CONTENT. If you want to let the user pick multiple files, add Intent#EXTRA_ALLOW_MULTIPLE. If you only need the user to save a single file, use Intent#ACTION_CREATE_DOCUMENT. If you use these APIs, you can pass the resulting Intent#getData() into fromSingleUri(Context, Uri) to work with that document.

If you really do need full access to an entire subtree of documents, start by launching Intent#ACTION_OPEN_DOCUMENT_TREE to let the user pick a directory. Then pass the resulting Intent#getData() into fromTreeUri(Context, Uri) to start working with the user selected tree.

As you navigate the tree of DocumentFile instances, you can always use getUri() to obtain the Uri representing the underlying document for that object, for use with ContentResolver#openInputStream(Uri), etc.

To simplify your code on devices running android.os.Build.VERSION_CODES#KITKAT or earlier, you can use fromFile(File) which emulates the behavior of a android.provider.DocumentsProvider.

Summary

Public methods
abstract Boolean

Indicates whether the current context is allowed to read from this file.

abstract Boolean

Indicates whether the current context is allowed to write to this file.

abstract DocumentFile?
createDirectory(@NonNull displayName: String)

Create a new directory as a direct child of this directory.

abstract DocumentFile?
createFile(@NonNull mimeType: String, @NonNull displayName: String)

Create a new document as a direct child of this directory.

abstract Boolean

Deletes this file.

abstract Boolean

Returns a boolean indicating whether this file can be found.

open DocumentFile?
findFile(@NonNull displayName: String)

Search through listFiles() for the first document matching the given display name.

open static DocumentFile
fromFile(@NonNull file: File)

Create a DocumentFile representing the filesystem tree rooted at the given File.

open static DocumentFile?
fromSingleUri(@NonNull context: Context, @NonNull singleUri: Uri)

Create a DocumentFile representing the single document at the given Uri.

open static DocumentFile?
fromTreeUri(@NonNull context: Context, @NonNull treeUri: Uri)

Create a DocumentFile representing the document tree rooted at the given Uri.

abstract String?

Return the display name of this document.

open DocumentFile?

Return the parent file of this document.

abstract String?

Return the MIME type of this document.

abstract Uri

Return a Uri for the underlying document represented by this file.

abstract Boolean

Indicates if this file represents a directory.

open static Boolean
isDocumentUri(@NonNull context: Context, @Nullable uri: Uri?)

Test if given Uri is backed by a android.provider.DocumentsProvider.

abstract Boolean

Indicates if this file represents a file.

abstract Boolean

Indicates if this file represents a virtual document.

abstract Long

Returns the time when this file was last modified, measured in milliseconds since January 1st, 1970, midnight.

abstract Long

Returns the length of this file in bytes.

abstract Array<DocumentFile!>

Returns an array of files contained in the directory represented by this file.

abstract Boolean
renameTo(@NonNull displayName: String)

Renames this file to displayName.

Public methods

canRead

abstract fun canRead(): Boolean

Indicates whether the current context is allowed to read from this file.

Return
Boolean true if this file can be read, false otherwise.

canWrite

abstract fun canWrite(): Boolean