After knowing the difference between _ID and AUDIO_ID (What is difference between _ID and AUDIO_ID column?), I'm changing my query to use AUDIO_ID instead, however I got some problem here.
Here's my code snippet that intended to query all audio files in external storage and make "Music" instances for each of them with embedded AUDIO_ID.
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cur = mContentResolver.query(uri, null,
MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null);
int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
int idColumn = cur.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID);
// All of the above columns return the right integers,
// except idColumn which returns -1 thus making my further code below throws an exception
do{
Music music = new Music(); // it's my music class
music.setArtist( cur.getString(artistColumn) );
music.setTitle( cur.getString(titleColumn) );
music.setId( cur.getLong(idColumn) ); // error here, because idColumn = -1 (column not found)
// ......
} while (cur.moveToNext())
Why can't I get the AUDIO_ID
column when querying MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
? Is there any way to retrieve it? (any workarounds appreciated)
I need it since my app needs absolute reference to audio files as explained in an answer comment here -> https://stackoverflow.com/a/17648131/670623
Thanks :)
Edit
I found a question with solution close to what I'm searching (it can get the AUDIO_ID), but it's intended to list all audio files from certain playlist. While my concern is querying the whole mediastore for AUDIO_IDs regardless from which playlist they are.
Given an Android music playlist name, how can one find the songs in the playlist?