MediaStore - mayurparmar2/AlarmDemo GitHub Wiki

fetchAllMusic

fetchAllMusic.kt
data class Music(
    val id: Long,
    val title: String,
    val artist: String,
    val path: String,
    val duration: Long,
    val album: String
)

private fun fetchAllMusic(): List<Music> {
    val musicList = mutableListOf<Music>()

    // Define projection (columns to retrieve)
    val projection = arrayOf(
        MediaStore.Audio.Media._ID,
        MediaStore.Audio.Media.TITLE,
        MediaStore.Audio.Media.ARTIST,
        MediaStore.Audio.Media.DATA,  // Path to the file
        MediaStore.Audio.Media.DURATION,
        MediaStore.Audio.Media.ALBUM
    )

    // Query the external content URI for audio files
    val selection = "${MediaStore.Audio.Media.IS_MUSIC} != 0"

    val cursor = contentResolver.query(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        projection,
        selection,
        null,
        null
    )

    cursor?.use {
        val idColumn = it.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)
        val titleColumn = it.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)
        val artistColumn = it.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)
        val dataColumn = it.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)
        val durationColumn = it.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)
        val albumColumn = it.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)

        while (it.moveToNext()) {
            // Get values from each row
            val id = it.getLong(idColumn)
            val title = it.getString(titleColumn)
            val artist = it.getString(artistColumn)
            val data = it.getString(dataColumn)
            val duration = it.getLong(durationColumn)
            val album = it.getString(albumColumn)

            // Add the music file to the list
            musicList.add(Music(id, title, artist, data, duration, album))
        }
    }

    return musicList
}

fetchAllContacts

fetchAllContacts.kt
data class Contact(
    val id: String,
    val name: String,
    val phoneNumbers: List<String>
)

private fun fetchAllContacts(): List<Contact> {
    val contactsList = mutableListOf<Contact>()

    // Define the projection (columns to retrieve)
    val projection = arrayOf(
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME
    )

    // Query the contacts database
    val cursor = contentResolver.query(
        ContactsContract.Contacts.CONTENT_URI,
        projection,
        null,
        null,
        null
    )

    cursor?.use {
        val idColumn = it.getColumnIndexOrThrow(ContactsContract.Contacts._ID)
        val nameColumn = it.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)

        while (it.moveToNext()) {
            val contactId = it.getString(idColumn)
            val displayName = it.getString(nameColumn)

            // Fetch phone numbers for each contact
            val phoneNumbers = fetchPhoneNumbers(contactId)

            // Add the contact to the list
            contactsList.add(Contact(contactId, displayName, phoneNumbers))
        }
    }

    return contactsList
}

private fun fetchPhoneNumbers(contactId: String): List<String> {
    val phoneNumbers = mutableListOf<String>()

    // Define the projection (columns to retrieve for phone numbers)
    val phoneProjection = arrayOf(ContactsContract.CommonDataKinds.Phone.NUMBER)

    // Query the phone numbers for the given contact ID
    val phoneCursor = contentResolver.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        phoneProjection,
        "${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} = ?",
        arrayOf(contactId),
        null
    )

    phoneCursor?.use {
        val numberColumn = it.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)

        while (it.moveToNext()) {
            val phoneNumber = it.getString(numberColumn)
            phoneNumbers.add(phoneNumber)
        }
    }

    return phoneNumbers
}
⚠️ **GitHub.com Fallback** ⚠️