Reputation: 23
I have this code but it doesnt print any mp3 files. would be appreciated if you could help
public void getMp3Songs() {
Uri uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
Cursor cursor = managedQuery(uri, null, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)+0 );
System.out.println(songName);
} while (cursor.moveToNext());
}
cursor.close();
}
}
Upvotes: 2
Views: 2824
Reputation: 1574
To get all music files on the device.
val selection = StringBuilder("is_music != 0 AND title != ''")
// Display audios in alphabetical order based on their display name.
val sortOrder = "${MediaStore.Audio.Media.DISPLAY_NAME} ASC"
val cursor: Cursor? = contentResolver.query(
EXTERNAL_CONTENT_URI,
null,
selection.toString(),
null,
sortOrder
)
if (cursor != null && cursor.moveToFirst()) {
do {
val id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID))
val title: String =
cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME))
val duration: Int =
cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION))
val size = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE))
val artist =
cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST))
} while (cursor.moveToNext())
}
cursor?.close()
Upvotes: 2