Getting real path from MediaStore API

Issue

I’m building a built-in file manager for my app and I’m using MediaStore to query for the files.

I got everything working, except that the URI I get back is not to Glide’s liking.

class java.io.FileNotFoundException: /external/images/media/37: open failed: ENOENT (No such file or directory)

I understand the issue at hand is that there’s no protocol added so glide doesn’t know how to read it, but I don’t know how to add the protocol, I was under the impression that using ContentUris.withAppendedId would handle that for me but it appears to not be.

val mediaList = mutableListOf<Uri>()
val query = buildQuery(folderId, onlyImages)

query?.use { cursor ->
    val columnIndexId = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID)
    val columnIndexMimeType = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.MIME_TYPE)
    val columnIndexDuration = cursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.DURATION)

    while (cursor.moveToNext())
    {
        val id = cursor.getLong(columnIndexId)
        val contentUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id)

        mediaList.add(contentUri)
    }
}

return mediaList

I’m trying to void using DATA as I already know that’s depreciated and I just need this solution work with Android 8+.

Solution

Are you converting the contentUri to a path? You shouldn’t be doing that. Your contentUri should look like it does below, and then Glide will accept it:

content://media/external/images/media/37

Answered By – Gavin Wright

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published