user6765242 user6765242 - 1 year ago 90
Android Question

Difference between File and DocumentFile in Java

What's exactly the difference between a

and a
in Java Android?

Answer Source

In short:

File is the "normal" Java file that one would normally know if even he never touched Android - i.e. abstraction for accessing underlying OS's files/directories.

DocumentFile was introduced to further abstract this in Android's Storage Access Framework (SAF) - it is either backed by "normal" file or by another abstraction called DocumentProvider and could be used to access both types of files in a more generic way. It is part of Google's continuing effort to 'hide' the underlying OS file/directory structure.

One approach would be to use both File (when accessing disk/card files) or DocumentFile (when accessing files backed from ContentProvider) but the more "modern" way would be to use always DocumentFile, which can represent a normal File by using fromFile(File)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download