Search code examples
androidkotlinormandroid-contentresolverkotlin-exposed

How do I use jetbrains exposed with android contentResolver/Mediastore


Im trying to make a simple android mediaplayer app that can be controlled from a distance. At the moment I'm trying to fix the issue of sending all the information on artists/albums/songs that are on the phone. At the moment I'm retrieving all the information as such:

private val contentResolver = activity.contentResolver!!

fun getAll():Set<Album>{
    val res = mutableSetOf<Album>()
    val cursor = contentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
            arrayOf(
                    MediaStore.Audio.Albums.ALBUM,
                    MediaStore.Audio.Albums.ALBUM_ART,
                    MediaStore.Audio.Albums.NUMBER_OF_SONGS,
                    MediaStore.Audio.Albums.ARTIST)
            ,null,null)
    if(cursor!!.moveToFirst())
        do {
            res.add(Album().apply {
                name = cursor.getString(0)
                if (!cursor.getString(1).isNullOrEmpty())
                    albumArtUri = Uri.parse(cursor.getString(1))
                songCount = cursor.getInt(2)
                artist = Artist().apply {
                    name = cursor.getString(3)
                }
            })
            cursor.moveToNext()
        }while (!cursor.isAfterLast)
    cursor.close()
    return res
}

Seeing that I'm using a cursor, I thought I was working with a kind of database (SQLite or so) As you can see, this is a lot of code for just a set of objects with little information; the album objects created don't have the songs in them. For this you'd need to start a new query, starting and a new URI. Now I thought I could use an ORM. So I can actually fill the album objects with a list of songs and so on. I decided to try Jetbrains Exposed, typed:

 val database = Database.connect(....)

and I'm at a loss, I don't know how to connect to this the database. I can't seem to find any examples on how to start with this.


Solution

  • Exposed is for JDBC. ContentResolver is not using JDBC, and the Cursor is not an object from JDBC. In general, Android does not use JDBC, in apps or at the OS level.