我开始了一个制作音乐播放器的项目。最初,我已经使用ApI级别29列出了所有mp3文件。但是我在创建媒体播放器时遇到了问题。每次显示的错误都是“ Found a null Uri”。我已经尝试过了...有趣的getAppExternalFilesDir():文件? {如果(Build.VERSION.SDK_INT> = 29){getExternalFilesDir(空)} else {// @ API 29中已弃用。// / storage / emulated / 0Environment.getExternalStorageDirectory()}}
fun ListDir(f: File) {
var files: Array<File>? = f.listFiles()
list.clear()
if (files != null) {
for (file: File in files) {
if (file.name.endsWith(".mp3")){
list.add(Model(file.name))
}
}
}
listview.setOnItemClickListener { parent:AdapterView<*>, view:View, position:Int, id:Long ->
if (position>=0) {
var textcopy: TextView = view.findViewById(R.id.foldername)
var namecopied: String = textcopy.text.toString()
mediaplayer = MediaPlayer()
var uri: Uri = Uri.parse((getAppExternalFilesDir().toString() + "/" + namecopied))
Toast.makeText(this, uri.toString(), Toast.LENGTH_SHORT).show()
mediaplayer = MediaPlayer.create(this, uri)
mediaplayer.prepare()
mediaplayer.start()
}
//where getAppExternalFilesDir() =getExternalFilesDir(null)
}
您的代码也未进行优化,您可以使用冷序列搜索文件,它会在运行时破坏文件的旧实例