Skip to content

[Android] isExists will return false on correct bookmarked Folder URIs #418

@licryle

Description

@licryle

Rebonjour!

Thanks again for your hard work :)

Working on migrating from Kotlin to Kotlin KMP, I've had to port over my previous persisted "non-bookmarked" Folder URIs to "bookmarking" in PlatformFile.

So I know the permission is correct, the app has access to it, however the following kept failing "Bookmark target is no longer accessible":
PlatformFile.fromBookmarkData(PlatformFile(context.contentResolver.persistedUriPermissions[0].uri).bookmarkData())

After investigating, I found the issue to be located with getDocumentFile():

private fun getDocumentFile(uri: Uri): DocumentFile? {
    return DocumentFile.fromSingleUri(FileKit.context, uri)
        ?: DocumentFile.fromTreeUri(FileKit.context, uri)
}

For folders, the DocumentFile.fromSingleUri() will succeed, however its isDirectory() or exists() are both false, even for a director!
This, in turns, makes PlatformFile.isExists() return false and makes PlatformFile.fromBookmarkData() throw.

As a result, I believe getDocumentFile should check if it's a directory or File first:

private fun getDocumentFile(uri: Uri): DocumentFile? {
    val tree = DocumentFile.fromTreeUri(FileKit.context, uri)

    return if (tree?.isDirectory == true) {
        tree
    } else {
        DocumentFile.fromSingleUri(FileKit.context, uri)
    }
}

I couldn't file a test battery, and gemini tells me what you did was "more correct" (and clearly more efficient), so don't trust my change by any means, but there's also something fishy that fromSingleUri() will succeed for a directory then reject at exists() or isDirectory()

Encore merci!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions