[英]How can I load all music files from external storage on Android?
I'm trying to read all songs from external storage using contentResolver?.query(), but every time I run app my query is empty.我正在尝试使用 contentResolver?.query() 从外部存储中读取所有歌曲,但每次运行应用程序时,我的查询都是空的。 It just goes from if(resultSet.moveToFirst()) to resultSet?.close().
它只是从 if(resultSet.moveToFirst()) 到 resultSet?.close()。 Here is my code and debug screen below:
这是我的代码和调试屏幕:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_tracks_tab, container, false)
permissionSetup()
val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
val selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"
val projection = arrayOf<String>(
MediaStore.Audio.AudioColumns._ID,
MediaStore.Audio.AudioColumns.TITLE,
MediaStore.Audio.ArtistColumns.ARTIST
)
val resultSet : Cursor? = activity?.applicationContext?.contentResolver?.query(uri, projection, selection, null, null)
if (resultSet != null) {
if(resultSet.moveToFirst()) {
do {
val url = resultSet.getString(0)
val artist = resultSet.getString(1)
val title = resultSet.getString(2)
listSong.add(SongInfo(title, artist, url))
} while (resultSet.moveToNext())
}
}
resultSet?.close()
enter image description here在此处输入图像描述
If it's important I'm writing in a tabFragment and my emulator device is Pixel 4XL with Android 11.0 API 30. Please, help me, I already tried everything I can find.
如果我在 tabFragment 中写入很重要,而我的模拟器设备是 Pixel 4XL,Android 11.0 API 30。请帮助我,我已经尝试了我能找到的一切。
Use this simple library!使用这个简单的库! I've tested it with Android 11 works totally fine!!!
我已经用 Android 测试了它 11 工作得很好!!!
https://github.com/CodeBoy722/MediaFacer https://github.com/CodeBoy722/MediaFacer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.