If I have a text file and i read it using DocumentFile when I call myDocumentFile.toString() I get its textual content as String.
That is not the behavior of DocumentFile
.
DocumentFile
itself is an abstract class. This is the source code to 1.0.1
of DocumentFile
, which right now is the most recent stable version. It has not changed since 2019. It has no toString()
implementation.
Your question does not contain details of where and how you obtained the DocumentFile
. Based on parts of your question, it appears that your object is an instance of TreeDocumentFile
, a concrete subclass of DocumentFile
. This is the source code to 1.0.1
of TreeDocumentFile
. It has not changed since 2019. It does not have a toString()
implementation.
Since neither DocumentFile
nor TreeDocumentFile
have toString()
, and DocumentFile
only extends the base Object
class, you inherit the Object
implementation of toString()
, which generates results like you show in your question (androidx.documentfile.provider.TreeDocumentFile@81660cb
).
Note that neither of these are affected by Android OS version, because they come from a library. Your implementation of DocumentFile
is the same on Android 4.0 as it is on Android 12. So, when I modified one of my sample apps to use ActivityResultContracts.OpenDocument
and DocumentFile.fromSingleUri()
, I got androidx.documentfile.provider.SingleDocumentFile@1e3ff95
from toString()
on Android 11.
But, if you like, update your question with a complete sample showing how you are getting the Uri
, how you are getting the DocumentFile
, and how you are getting its text content via toString()
, and we can try it out on our devices.
What is the most concise way to get its content as String?
TreeDocumentFile
is for a document tree, not a document. It has no content.
For a DocumentFile
pointing to a document, call getUri()
on the DocumentFile
, pass that to openInputStream()
on a ContentResolver
, and read in the InputStream
.