0

From here: https://stackoverflow.com/a/70933975/13519865

if I,

private val dirRequest = registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri ->
    if (uri != null) {

DocumentsContract.getDocumentId(uri)

}

}

I get

java.lang.IllegalArgumentException: Invalid URI: content://com.android.externalstorage.documents/tree/9C33-6BBD%3AA
at android.provider.DocumentsContract.getDocumentId(DocumentsContract.java:1278)

I need documentId for,

DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, documentId)
Cyber Avater
  • 1,494
  • 2
  • 9
  • 25
  • 1
    The ID you get back is the `rootId`, not a `documentId` of an individual file in that directory. Is there a reason you're trying to get a documentId for a tree Uri? – ianhanniballake Jul 28 '23 at 22:10
  • List entries of that directory – Cyber Avater Jul 29 '23 at 05:02
  • You could use `DocumentFile.fromTreeUri().listFiles()` to list the entries. If you do not wish to use `DocumentFile`, you could read its source code to see how it does its job. – CommonsWare Aug 14 '23 at 17:10
  • @CommonsWare, Hi! thanks for responding. Check this function https://github.com/google/modernstorage/blob/main/storage/src/main/java/com/google/modernstorage/storage/AndroidFileSystem.kt#L145 I tried to use this library and I got the error I mentioned. – Cyber Avater Aug 14 '23 at 18:35
  • @CyberAvater: That code is maintained by Google developers. You might wish to file a bug report with a [mcve] showing your problem. – CommonsWare Aug 14 '23 at 19:10
  • @CommonsWare your suggested method `DocumentFile.fromTreeUri().listFiles()` worked for me. Please add it as a nice answer (with any advice you have and explanations) and I'll accept it. And regarding opening an issue for their lib, I think it's dead. https://github.com/google/modernstorage/issues/107 – Cyber Avater Aug 18 '23 at 16:41
  • I recommend that you answer your own question with your findings! – CommonsWare Aug 18 '23 at 16:56
  • I may do that. However, your answer helped and I think you deserve the bounty. – Cyber Avater Aug 18 '23 at 17:16
  • The Google sample doesn't use `rootUri` from `OpenDocumentTree` (`content://com.android.externalstorage.documents/tree`), but with an Uri from `okio.Path.toUri()` (probably a `file://` Uri). It's not the same as your code. – Eugen Pechanec Aug 20 '23 at 15:52

0 Answers0