gpt4 book ai didi

android - Audio.Media.EXTERNAL_CONTENT_URI 列表在文件删除后不更新

转载 作者:太空狗 更新时间:2023-10-29 15:00:49 26 4
gpt4 key购买 nike

我正在为所有音乐文件查询 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI。它工作得很好,但每当我想从我的 sdCard 中删除音乐文件时,列表不会更新。知道如何更新列表吗?

这是我获取音乐文件的代码:

    Cursor cursor;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";


cursor = getActivity().getContentResolver().query(uri, {"*"}, selection, null, null);

if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String songName = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));


String path = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));


String albumName = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
int albumId = cursor
.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

songs.add(new SongItem(albumName, albumId, path, songname));

} while (cursor.moveToNext());
}
}

最佳答案

新添加的文件被添加到游标中,被删除的文件实际上并不会被删除。

为此,我们需要检查文件是否可用。如果不可用,请使用内容提供程序从数据库中删除它

这就是我解决问题的方法:

        Uri files= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;    
Cursor cursor= managedQuery(files, STAR, null,null,null);
cursor.moveToFirst();
for(int r= 0; r<cursor.getCount(); r++, cursor.moveToNext()){
int i = cursor.getInt(0);
File file = new File(cur.getString(cur.getColumnIndex(MediaStore.Audio.Media.DATA)));

if(file.exists()){
// keep files if it is empty
list.add("Keeping : " + cursor.getString(2) + " : id(" + i + ")");
}else{
// delete any play-lists with a data length of '0'
Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, i);
getContentResolver().delete(uri, null, null);
list.add("Deleted : " + cursor.getString(2) + " : id(" + i + ")");
}
}
cursor.close();

关于android - Audio.Media.EXTERNAL_CONTENT_URI 列表在文件删除后不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27012510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com