Skip to content

DocumentFileCompat.fromSingleUri "giving Exception while building the Cursor" #19

@Anthonyy232

Description

@Anthonyy232

Hi, I'm encountering this issue with my code using this piece of code.

fun isDirectory(context: Context, uriString: String?): Boolean {
    val uri = uriString?.toUri() ?: return false
    return try {
        val valid = DocumentFileCompat.fromSingleUri(context, uri)?.isDirectory() ?: false
        Log.d("WallpaperUtil", "URI is directory: $uri")
        valid
    } catch (e: Exception) {
        Log.e("WallpaperUtil", "Error checking if URI is a directory: ", e)
        DocumentFile.fromSingleUri(context, uri)?.isDirectory ?: false
    }
}

The violating piece of code produces this output:

2025-04-14 13:36:22.453 11686-11736 DocumentFileCompat      com.anthonyla.paperize               E  Exception while building the Cursor: Unsupported Uri content://com.android.externalstorage.documents/tree/primary%3APictures%2Ftweats
2025-04-14 13:36:22.453 11686-11736 WallpaperUtil           com.anthonyla.paperize               D  URI is directory: content://com.android.externalstorage.documents/tree/primary%3APictures%2Ftweats

So it seems like there is something wrong when it is trying to make a cursor from the URI: "content://com.android.externalstorage.documents/tree/primary%3APictures%2Ftweats". I can confirm that the specific expression yielding the error is the fromSingleUri, NOT isDirectory. Furthermore, this piece of code works perfectly if I replace DocumentFileCompat with DocumentFile, so I think the URI is correct. I have tested it and it still occurs if the folder name is different (eg: Pictures/test, or Pictures/tweats). It also occurs under a different main-subfolder (eg: Test/tweats, Test/*.images).

Let me know if you need anything from me!
https://github.com/Anthonyy232/Paperize/tree/docFileCompat_test (if you want to test, just have to build, run the app, supply a folder through an album, and then select the album for the wallpaper changer. It should show up in the error log as DocumentFileCompat

Tested on Android 15 emulator through AS

I

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