Android重命名后光标找不到mp3文件
Android Cursor can't find mp3 file after I rename it
我正在研究 Mp3 播放器。我正在为此使用 curosr。我的问题是,在我重命名 pm3 文件后,光标无法找到该 mp3 文件。我查看了放置 mp3 文件的文件夹并将其重命名。你有什么主意吗?我正在使用光标获取音乐:
@RequiresApi(Build.VERSION_CODES.Q)
private fun getAllAudio(): ArrayList<Music> {
val tempAudioList = ArrayList<Music>()
val selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"
val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
Log.d("SONGS", uri.path!!)
val projection = arrayOf(
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.SIZE,
MediaStore.Audio.Media._ID
)
val cursor = this.contentResolver.query(
uri,
projection,
selection,
null,
null
)
if (cursor != null) {
while (cursor.moveToNext()) {
val path = cursor.getString(0)
val title = cursor.getString(1)
val artist = cursor.getString(2)
val album = cursor.getString(3)
val duration = cursor.getString(4)
val size = cursor.getString(5)
val id = cursor.getString(6)
val art = getAlbumArt(path)
val file = File(path)
Log.d("ALLSONGS", path)
if (file.exists()) {
val musicFiles = Music(path, title, artist, album, duration, size, id, art)
tempAudioList.add(musicFiles)
}
}
cursor.close()
}
return tempAudioList
}
我正在申请权限:
private fun askStoragePermissions() {
Dexter.withActivity(this)
.withPermissions(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
.withListener(object : MultiplePermissionsListener {
@RequiresApi(Build.VERSION_CODES.Q)
override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
if (report!!.areAllPermissionsGranted()) {
Log.d("MAIN_ACTIVITY", "Permission granted")
musicList = getAllAudio()
}
if (report.isAnyPermissionPermanentlyDenied) {
}
}
override fun onPermissionRationaleShouldBeShown(
permissions: MutableList<PermissionRequest>?,
token: PermissionToken?
) {
token!!.continuePermissionRequest()
}
})
.onSameThread()
.check()
}
清单:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
正在重命名文件:
val newName = mView.edit_txt_new_title.text.toString()
val path = ArrayList(music.path.split('/'))
path[path.size - 1] = newName
var newPath = ""
for (i in 0 until path.size - 1) {
newPath += "${path[i]}/"
}
newPath += "$newName.mp3"
val file = File(music.path)
if (file.renameTo(File(newPath)))
if (File(newPath).renameTo(File(newPath)))
Toast.makeText(context, "DONE", Toast.LENGTH_LONG).show()
Log.d("RENAME", "$newPath -- ${music.path}")
重命名文件后,您必须更新 MediaStore 以在成功重命名文件后使用以下方法执行此操作:
private fun updateMediaStore(music_Id: String, newPath: String, newName: String) {
val values = ContentValues()
values.put(MediaStore.Audio.Media.DATA, newPath)
values.put(MediaStore.Audio.Media.DISPLAY_NAME, newName)
val uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, music_Id.toLong())
contentResolver.update(uri, values, null, null)
}
在上面的方法中我更新了路径和显示名称,你可以在这里使用:
/*
rest of your code
*/
val file = File(music.path)
if (file.renameTo(File(newPath)))
if (File(newPath).renameTo(File(newPath))) {
Log.d("RENAME", "$newPath -- ${music.path}")
Toast.makeText(context, "DONE", Toast.LENGTH_LONG).show()
updateMediaStore(music.id,newPath,newName)
}
我正在研究 Mp3 播放器。我正在为此使用 curosr。我的问题是,在我重命名 pm3 文件后,光标无法找到该 mp3 文件。我查看了放置 mp3 文件的文件夹并将其重命名。你有什么主意吗?我正在使用光标获取音乐:
@RequiresApi(Build.VERSION_CODES.Q)
private fun getAllAudio(): ArrayList<Music> {
val tempAudioList = ArrayList<Music>()
val selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"
val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
Log.d("SONGS", uri.path!!)
val projection = arrayOf(
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.SIZE,
MediaStore.Audio.Media._ID
)
val cursor = this.contentResolver.query(
uri,
projection,
selection,
null,
null
)
if (cursor != null) {
while (cursor.moveToNext()) {
val path = cursor.getString(0)
val title = cursor.getString(1)
val artist = cursor.getString(2)
val album = cursor.getString(3)
val duration = cursor.getString(4)
val size = cursor.getString(5)
val id = cursor.getString(6)
val art = getAlbumArt(path)
val file = File(path)
Log.d("ALLSONGS", path)
if (file.exists()) {
val musicFiles = Music(path, title, artist, album, duration, size, id, art)
tempAudioList.add(musicFiles)
}
}
cursor.close()
}
return tempAudioList
}
我正在申请权限:
private fun askStoragePermissions() {
Dexter.withActivity(this)
.withPermissions(
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
)
.withListener(object : MultiplePermissionsListener {
@RequiresApi(Build.VERSION_CODES.Q)
override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
if (report!!.areAllPermissionsGranted()) {
Log.d("MAIN_ACTIVITY", "Permission granted")
musicList = getAllAudio()
}
if (report.isAnyPermissionPermanentlyDenied) {
}
}
override fun onPermissionRationaleShouldBeShown(
permissions: MutableList<PermissionRequest>?,
token: PermissionToken?
) {
token!!.continuePermissionRequest()
}
})
.onSameThread()
.check()
}
清单:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
正在重命名文件:
val newName = mView.edit_txt_new_title.text.toString()
val path = ArrayList(music.path.split('/'))
path[path.size - 1] = newName
var newPath = ""
for (i in 0 until path.size - 1) {
newPath += "${path[i]}/"
}
newPath += "$newName.mp3"
val file = File(music.path)
if (file.renameTo(File(newPath)))
if (File(newPath).renameTo(File(newPath)))
Toast.makeText(context, "DONE", Toast.LENGTH_LONG).show()
Log.d("RENAME", "$newPath -- ${music.path}")
重命名文件后,您必须更新 MediaStore 以在成功重命名文件后使用以下方法执行此操作:
private fun updateMediaStore(music_Id: String, newPath: String, newName: String) {
val values = ContentValues()
values.put(MediaStore.Audio.Media.DATA, newPath)
values.put(MediaStore.Audio.Media.DISPLAY_NAME, newName)
val uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, music_Id.toLong())
contentResolver.update(uri, values, null, null)
}
在上面的方法中我更新了路径和显示名称,你可以在这里使用:
/*
rest of your code
*/
val file = File(music.path)
if (file.renameTo(File(newPath)))
if (File(newPath).renameTo(File(newPath))) {
Log.d("RENAME", "$newPath -- ${music.path}")
Toast.makeText(context, "DONE", Toast.LENGTH_LONG).show()
updateMediaStore(music.id,newPath,newName)
}