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)