Android MediaStore query MediaStore.MediaColumns.TITLE column is null for some files
Asked Answered
P

1

12

I'm making a query on the Android's MediaStore Files Database -MediaStore.Files.getContentUri("external") - and, for some specific folders, both the MediaStore.MediaColumns.TITLE, and MediaStore.MediaColumns.DISPLAY_NAME are null while for other folders this value exists. I couldn't find any documentation about MediaStore.MediaColumns.TITLE being possibly null.

This happens for a few internal Android directories, such as these:

_data: /storage/emulated/0/Music, title: null, _display_name: null
_data: /storage/emulated/0/Notifications, title: null, _display_name: null
_data: /storage/emulated/0/Pictures, title: null, _display_name: null

However, for some other folders, the title is there:

 _data: /storage/emulated/0/Android, title: Android, _display_name: null
 _data: /storage/emulated/0/DCIM, title: DCIM, _display_name: null
 _data: /storage/emulated/0/Download, title: Download, _display_name: null

All data comes directly from the MediaStore query.

I am aware I could work directly with the Data but I'm trying to sort the query according to the TITLE, which leads to incorrect results given that some are null.

Is this an expected behaviour? How to deal with it and retrieve all files correctly sorted by title?

Pietro answered 5/5, 2016 at 10:41 Comment(5)
I'm wondering if this should be the way go to: #8188328Pietro
You are getting null title or display name ?Boil
@HirenPatel when it is null, it is null for both title and display name. Only data field is filled.Pietro
are you working for only audio files or all types of files ?Boil
@HirenPatel files of all types. MediaStore.Files.getContentUri("external");Pietro
F
1

Looks like this is an android limitation. You could always try to do a order by such as this:

select substr(
    substr(substr(MediaStore.MediaColumns.DATA, instr(MediaStore.MediaColumns.DATA, '/') + 1), instr(substr(MediaStore.MediaColumns.DATA, instr(MediaStore.MediaColumns.DATA, '/') + 1), '/') + 1),
    instr(substr(substr(MediaStore.MediaColumns.DATA, instr(MediaStore.MediaColumns.DATA, '/') + 1), instr(substr(MediaStore.MediaColumns.DATA, instr(MediaStore.MediaColumns.DATA, '/') + 1), '/') + 1), '/') + 1
    ) from tablename;

Note that this is not recursive, but only works for up to 3 '/'.

This could be done recursively but it was only introduced later on Android

Floe answered 16/5, 2016 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.