从MediaStore API获得真正的道路

发布于 2025-02-06 03:04:53 字数 1039 浏览 0 评论 0原文

我正在为我的应用构建一个内置文件管理器,并且正在使用MediaStore查询文件。

我得到了一切,除了我回来的URI并不是要滑行的喜欢。

类Java.io.filenotfoundexception:/external/images/媒体/37:打开失败:enoent(无此类文件或目录)

我理解当前的问题是没有添加协议,因此Glide不知道如何读取它,但是我不知道如何添加协议,我的印象是,使用contenturis.withappendedId会为我处理这一点,但似乎并非如此。

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

我正试图使用​​数据丢失,因为我已经知道这已经折旧,我只需要与Android 8+一起使用此解决方案。

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+.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北斗星光 2025-02-13 03:04:53

您是将contenturi转换为路径吗?你不应该这样做。您的contenturi应该看起来像在下面一样,然后Glide将接受它:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文