【问题标题】:MediaStore returns null when querying for DISPLAY_NAMEMediaStore 在查询 DISPLAY_NAME 时返回 null
【发布时间】:2020-09-27 08:16:24
【问题描述】:

在极少数情况下,MediaStore 在尝试使用以下代码 sn-p 查询 MediaStore.Video.VideoColumns.DISPLAY_NAME 时返回 null

String displayName = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DISPLAY_NAME));

我猜它可能与文件名有关,因为它只在少数设备上报告过。但是我假设文件显示名称不能为空,所以MediaStore 返回null 有点奇怪。有没有人遇到过类似的问题?

【问题讨论】:

  • 对于 Android 9 及更低版本可以为空。但在 Android 10+ 上,它是非空的。
  • @AnggrayudiH 它怎么会变成空?我正在查看MediaProvider,似乎DATA 列用于在直接查询返回null 时间接查找DISPLAY_NAME。对于存储中存在的文件,DATA 字段如何变为空?
  • DATA 字段在 Android 10 中已被弃用,可能包含也可能不包含任何数据。最合乎逻辑的是,如果 DISPLAY_NAME 为空,则 TITLE 字段应该是最合乎逻辑的替代字段,尽管此字段不包含媒体扩展名。

标签: android mediastore


【解决方案1】:

我们在一台设备上遇到了同样的问题。

如果 DISPLAY_NAME 为空,则可以使用 TITLE。

String displayName = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DISPLAY_NAME));
if (displayName == null) {
    displayName = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.TITLE));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    相关资源
    最近更新 更多