0

If I have a text file and i read it using DocumentFile when I call myDocumentFile.toString() I get its textual content as String. In Android 12 if I call myDocumentFile.toString() on a text file loaded as DocumentFile rather than textual content I get as String something like

androidx.documentfile.provider.TreeDocumentFile@81660cb

It's not clear why this change of behaviour.

What is the most concise way to get its content as String?

AndreaF
  • 11,975
  • 27
  • 102
  • 168

1 Answers1

0

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I assure you that If I call it on previous versions `.toString()` on my `DocumentFile` object obtained with `findFile()` I get the content String, It isn't a my fantasy, If i call it in Android 12 I get that `androidx.documentfile.provider.TreeDocumentFile@81660cb` so certainly something is changed – AndreaF Nov 09 '21 at 00:59
  • 1
    @AndreaF, Interesting. Please show your code. – blackapps Nov 09 '21 at 08:06
  • @AndreaF: You can look at the source code to `DocumentFile` and its related classes, and you will not find any code in there that matches your description. And, it does not change the fact that a document tree does not have content to print. You did not provide a [mcve]. But, as I suggested, you could create a sample project that proves your claims -- I for one would be very happy to look at it. – CommonsWare Nov 09 '21 at 12:49