我有一个音乐应用程序。现在,我想将我的音乐应用程序升级到 Android 11。我使用“光标”来搜索音乐文件。但是,我收到错误。我搜索了错误消息并执行了此解决方案,但是任何解决方案都没有解决我的问题。我将分享代码片段和问题详细信息。有人可以帮我解决这个错误吗?
代码片段
ContentResolver musicResolver = application.getContentResolver();
String SONG_WHERE_CLAUSE;
Cursor musicCursor;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
String[] selectionArgs;
if (scanAll) {
SONG_WHERE_CLAUSE = "duration > ?";
selectionArgs = new String[1];
selectionArgs[0] = "5000";
} else {
SONG_WHERE_CLAUSE = MediaStore.Audio.AudioColumns.DURATION + " > ? AND " + MediaStore.Audio.AudioColumns.IS_MUSIC + " = ?";
selectionArgs = new String[2];
selectionArgs[0] = "3000";
selectionArgs[1] = "1";
}
Bundle bundle = new Bundle();
bundle.putInt(ContentResolver.QUERY_ARG_LIMIT, 50);
bundle.putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING);
bundle.putString(ContentResolver.QUERY_ARG_SORT_COLUMNS, MediaStore.MediaColumns.DATE_ADDED);
bundle.putString(ContentResolver.QUERY_ARG_SQL_SELECTION, SONG_WHERE_CLAUSE);
bundle.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
musicCursor = application.getContentResolver().query(MediaStore.Files.getContentUri("external"), null, bundle, null);
} else {
SONG_WHERE_CLAUSE = "duration > 3000 AND " + MediaStore.Audio.Media.IS_MUSIC + " = 1";
if (scanAll)
SONG_WHERE_CLAUSE = "duration > 5000";
musicCursor = musicResolver.query(musicUri, null, SONG_WHERE_CLAUSE, null, MEDIA_SORT);
}
错误详情
java.lang.IllegalArgumentException: Invalid token is_music
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:472)
at android.content.ContentResolver.query(ContentResolver.java:1183)
我在这行遇到错误; musicCursor = application.getContentResolver().query(MediaStore.Files.getContentUri("external"), null, bundle, null);
谢谢。
我也面临着同样的问题。 要解决此问题,只需将您的 uri 从
MediaStore.Files.getContentUri("external")
更改为 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
或 MediaStore.Audio.Media.getContentUri("external")
。
这是因为可用的键或列取决于正在查询的 Uri。