0

I am trying to implement a recyclerview which will show all the music files in my android device with a 10 per page pagination and contains a search text. I am using content resolver for this. My application is targeting till android 12. So my existing code is like:

fun getAllMusicFilesInDevice(offset: Int, searchText: String){
val tempAudioList: MutableList<Album> = ArrayList()
        val uri: Uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
            MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
        else
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
        val projection = arrayOf(
            MediaStore.Audio.AudioColumns.DATA,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.AudioColumns.ALBUM,
            MediaStore.Audio.ArtistColumns.ARTIST,
        )
        val selection = MediaStore.Audio.Media.DISPLAY_NAME + " LIKE ?"
        val selectionArgs = arrayOf(searchText)
        val sortOrder =
            "${MediaStore.Audio.Media.DISPLAY_NAME} ASC LIMIT $PAGE_SIZE OFFSET $offset"
        val c = mContext.contentResolver.query(
            uri,
            projection,
            selection,
            selectionArgs,
            sortOrder
        )
        if (c != null) {
            while (c.moveToNext()) {
                try {
                    val path: String = c.getString(0)
                    val songName = c.getString(1)
                    val album: String = c.getString(2)
                    val artist: String = c.getString(3)
                    val extension = File(path).extension
                    if (isMusicFile(extension)) {
                        isMusicPresentInPlaylist(path, playlistDb) {
                            val audioModel =
                                Album(
                                    name = songName,
                                    path = path,
                                    artist = artist,
                                    album = album
                                )
                            tempAudioList.add(audioModel)
                        }
                    }
                } catch (ex: Exception) {
                    
                }
            }
            c.close()
        }}

This code does not give back any result and makes the app behave like its stuck. Please help me out on the mistakes I am making and anything I am doing wrong out here so that I get this function working.

UPDATE CURSOR CODE

val c = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val limit = Bundle().apply {
                // Limit & Offset
                putInt(ContentResolver.QUERY_ARG_LIMIT, AUDIO_PAGE_SIZE)
                putInt(ContentResolver.QUERY_ARG_OFFSET, offset)
            }
            mContext.contentResolver.query(uri, projection, limit, null)
        } else
            mContext.contentResolver.query(
                uri,
                projection,
                null,
                null,
                "${MediaStore.Audio.Media.DISPLAY_NAME} ASC LIMIT $AUDIO_PAGE_SIZE OFFSET $offset"
            )

Note: this code is running in a background thread.

theAndDev
  • 662
  • 2
  • 11
  • 33
  • All these clauses do not work for MediaStore. Try without. – blackapps Feb 21 '22 at 14:36
  • I found a sample here: https://developer.android.com/training/data-storage/shared/media. this shows using clauses on the video media and I wrote the above code accordingly. @blackapps – theAndDev Feb 21 '22 at 17:18
  • You see more then i do. I see no LIMIT. I see no offset. – blackapps Feb 21 '22 at 17:51
  • @blackapps you mentioned **all these clauses**, not which clauses :p! So am I supposed to basically filter on the response list that I get for my searchText? – theAndDev Feb 21 '22 at 18:13
  • A lot of filtering you should do yourself indeed. And if there are for instance a 100K images on the phone you will fail time. – blackapps Feb 21 '22 at 18:19
  • @blackapps What if I told you I had a way of making the LIMIT and OFFSET work for android Q and above? Will you help me on figuring out on how to make LIKE work? – theAndDev Feb 21 '22 at 18:19
  • Please post the code. But as i'm laying under a palm tree right now i dont know when i could do that. – blackapps Feb 21 '22 at 18:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242241/discussion-between-theanddev-and-blackapps). – theAndDev Feb 21 '22 at 18:26
  • This is a forum. No chat. – blackapps Feb 21 '22 at 18:35
  • @blackapps the chat is provided by the forum itself. No matter, I have added the snippet in my question. Please help me out as soon as you can. – theAndDev Feb 21 '22 at 18:37
  • ContentResolver.QUERY_ARG_LIMIT and OFFSET worked here only above Q. I think for the rest you should use (untested) QUERY_ARG_SQL_SELECTION in combination with QUERY_ARG_SQL_SELECTION_ARGS. But as said before I'm not that far yet.. All in: https://developer.android.com/reference/android/content/ContentResolver – blackapps Feb 22 '22 at 09:30
  • An example for using the selection arguments is here: https://stackoverflow.com/questions/10390577/limiting-number-of-rows-in-a-contentresolver-query-function – blackapps Feb 22 '22 at 09:37
  • Got it to work with the `like`. I think you will soon be ready if you use last link. – blackapps Feb 22 '22 at 10:08

0 Answers0