0

When I run my application on my device(API Level 26). It shows me error but when I run my application in my another device (API Level 30) it's working fine. I am trying to access the songs path to the folder fragment but after clicking the folder fragment it shows me error.

class AllFolders : Fragment() {

    lateinit var folderRv: RecyclerView

    // var folderList: ArrayList<FolderModal> = ArrayList()
    lateinit var folderAdapter: FolderAdapter
    lateinit var folderSearch: SearchView
    lateinit var albumFilter: ImageButton
    var folderList: ArrayList<FolderModal> = ArrayList()

    companion object {
        var hashedFolders: HashSet<FolderModal> = HashSet()

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? { // Inflate the layout for this fragment var view: View = inflater.inflate(R.layout.fragment_all_folders, container, false)
        folderSearch = view.findViewById(R.id.folderSearch)
        albumFilter = view.findViewById(R.id.listFilter)
        folderSearch.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {

                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                //  Toast.makeText(context,"album list: "+ AllAlbums.albumList.size.toString(),Toast.LENGTH_SHORT).show()
                folderAdapter.getFilter().filter(newText)
                return false
            }

        })

        //bottomsheet
        albumFilter.setOnClickListener {
            openBottomSheetForFilter()
        }

        AllFolderBackgroundTask().execute()

        return view

    }

    inner class AllFolderBackgroundTask : AsyncTask<String, Void, String>() {
        @RequiresApi(Build.VERSION_CODES.Q)
        override fun doInBackground(vararg params: String?): String {
            if (folderList.isEmpty()) {
                getAllFolderWithPath()
            }

            return "dataFetched"
        }

        override fun onPostExecute(result: String?) {
            super.onPostExecute(result)
            //get ids
            folderRv = view!!.findViewById(R.id.allFolderRv)


            folderRv.layoutManager = LinearLayoutManager(requireContext())
            folderAdapter = FolderAdapter()
            folderRv.adapter = folderAdapter
            folderList = ArrayList(hashedFolders)
            folderList.sortBy { it.folderName }
            folderAdapter.getAllFolder(requireContext(), folderList, view!!)
        }
    }

    @RequiresApi(Build.VERSION_CODES.Q)
    private fun getAllFolderWithPath() {
        val uri: Uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
        val projection = arrayOf(

            MediaStore.Audio.Media.RELATIVE_PATH,
            MediaStore.Audio.AudioColumns.DISPLAY_NAME,
            MediaStore.Audio.AudioColumns.DATE_ADDED,
        )
        val c: Cursor? = context?.contentResolver?.query(
            uri,
            projection,
            null,
            null,
            //   MediaStore.Audio.AudioColumns.BUCKET_DISPLAY_NAME + " DESC"
        )

        if (c != null) {
            while (c.moveToNext()) {

                val FolderModal: FolderModal
                val path: String = c.getString(0)
                val folder: String = c.getString(1)
                val folderAdded: String = c.getString(2)
                FolderModal =
                    FolderModal(folderPath = path, folderName = folder)
                hashedFolders.add(FolderModal)

            }
            c.close()
        }
    }
}

Error:

Caused by: android.database.sqlite.SQLiteException: no such column: relative_path (code 1): , while compiling: SELECT relative_path, display_name, date_added FROM audio
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:418)
    at android.content.ContentResolver.query(ContentResolver.java:858)
    at android.content.ContentResolver.query(ContentResolver.java:779)
    at android.content.ContentResolver.query(ContentResolver.java:737)
    at com.example.app.AllFragments.AllFolders.getAllFolderWithPath(AllFolders.kt:107)
    at com.example.app.AllFragments.AllFolders.access$getAllFolderWithPath(AllFolders.kt:23)
    at com.example.app.AllFragments.AllFolders$AllFolderBackgroundTask.doInBackground(AllFolders.kt:74)
    at com.example.app.AllFragments.AllFolders$AllFolderBackgroundTask.doInBackground(AllFolders.kt:70)
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Priya
  • 1
  • 2

1 Answers1

-1

The reason of the exception is BUCKET_DISPLAY_NAME. It is added in API 29. Before this we were using DISPLAY_NAME for API 28 & below. Please refer to the docs BUCKET_DISPLAY_NAME.

For solution you can write conditions according to current API level. And for getting folder name, you can use RELATIVE_PATH.

android.database.sqlite.SQLiteException: no such column: bucket_display_name

Code Chef
  • 31
  • 1
  • 7
  • but I didn't use BUCKET_DISPLAY-NAME – Priya Mar 28 '22 at 10:43
  • yes I am saying that for ApI 29 and below you can use DISPLAY_NAME and For Higher than 29 you have to use BUCKET_DISPLAY_NAME instead of DISPLAY_NAME. – Code Chef Mar 28 '22 at 11:22
  • Is there any another way to use something else instead of Relative path (Which was added for above API level 29) . So I am unable to use relative path for below API level 29? – Priya Mar 28 '22 at 14:35