简体   繁体   English

Android:从MediaStore.Audio.Genres.Members表中删除条目

[英]Android: deleting entries from MediaStore.Audio.Genres.Members table

I am writing an Android music player capable of editing MP3 song genres. 我正在编写一个能够编辑MP3歌曲类型的Android音乐播放器。

The procedure I'm following is: 我要遵循的过程是:

  1. Edit the genre tag in the MP3 file itself 编辑MP3文件本身中的流派标签
  2. Update the SqlLite MediaStore.Audio.Genres tables to reflect the changes (I do not want to trigger a lengthy 'rescan' operation for Android to pickup the changes itself) 更新SqlLite MediaStore.Audio.Genres表以反映更改(我不想为Android触发冗长的“重新扫描”操作以自行获取更改)

My research suggests I need to modify MediaStore.Audio.Genres.Members table (ie this is the table I use to fetch the genre info). 我的研究表明,我需要修改MediaStore.Audio.Genres.Members表(即我用来获取类型信息的表)。

My issue is a seemingly simple delete operation is not working as expected. 我的问题是看似简单的删除操作无法正常工作。

Note: I do know NOT to use hardcoded URIs as shown, but this is just test code... 注意:我不知道不要使用所示的硬编码URI,但这只是测试代码...

String uriStr = "content://media/external/audio/genres/2/members";
Uri uri = Uri.parse(uriStr);

// Query the table for the row with ID=5448
Cursor cursor = context.getContentResolver().query(uri, null, BaseColumns._ID + "=5448", null, null);
Log.v("Test", "Row count: " + cursor.getCount());
cursor.close();

// Try to delete row 5448
int rowsCount = context.getContentResolver().delete(uri, BaseColumns._ID + "=5448", null);
Log.v("Test", "Rows deleted: " + rowsCount);

// Try (again) to delete row 5448
rowsCount = mService.getContentResolver().delete(uri, BaseColumns._ID + "=?", new String[]{"5448"});
Log.v("Test", "Rows deleted: " + rowsCount);

// Query (again) the table for the row with ID=5448         
Cursor cursor1 = mService.getContentResolver().query(uri, null, BaseColumns._ID + "=5448", null, null);
Log.v("Test", "Row count: " + cursor1.getCount());
cursor1.close();

The log of the above code is... 上面代码的日志是...

Row count: 1
Rows deleted: 0
Rows deleted: 0
Row count: 1

Why does the above query return a valid entry, but the deletes fail??? 为什么上面的查询返回一个有效的条目,但是删除失败?

It does not make sense to edit the table. 编辑表格没有任何意义。 The android will automatically update this with what it finds on your tagged music. android会自动使用您标记的音乐中的内容进行更新。 Why not look at the problem from an mp3 perspective? 为什么不从mp3角度看问题呢? Present the user with a list of Genres, offer a replace function and then update the tracks with the new genre, 向用户显示类型列表,提供替换功能,然后使用新类型更新曲目,

也许您不需要更多,但是如果您这样做,我可以为我提供解决方案的建议:在显示流派时,只需检查每种流派具有的曲目数,如果为0,就不要显示此流派。

You would rather use the AUDIO_ID column and not BaseColumns._ID because it is possible that row ids are updated each time you delete a row. 您宁愿使用AUDIO_ID列,而不要使用BaseColumns._ID,因为每次删除行时都有可能更新行ID。

Here is what I do in my own app (tested and working fine on Android 4.0.4) : 这是我在自己的应用中所做的工作(在Android 4.0.4上经过测试并可以正常工作):

context.getContentResolver().delete(MediaStore.Audio.Genres.Members.getContentUri(
                "external", genreId), MediaStore.Audio.Genres.Members.AUDIO_ID + " = " + song.getId(), null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM