Home > Software engineering >  Getting real path from MediaStore API
Getting real path from MediaStore API

Time:06-10

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 .

CodePudding user response:

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
  • Related