我构建了一个简单的查询来在Android模拟器的SD卡上查找音乐。我正在使用Android Studio。我使用adb push在sdcard上放了3首歌曲。但是,查询未找到音乐文件。以下是“音乐”文件夹中SD卡上文件的图像:
以下是我用来执行查询的代码:
ContentResolver musicResolver = getContentResolver();
Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
// Now we can iterate over the results, first checking that we have valid data:
if(musicCursor!=null && musicCursor.moveToFirst()){
// We first retrieve the column indexes for the data items that we are interested in for each song
int titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
// then we use these to create a new Song object and add it to the list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
allSongsPlaylist.add(new Song(thisId, thisTitle, thisArtist));
}
while (musicCursor.moveToNext());
}
musicCursor不为null,但musicCursor.moveToNext()返回0。
我做错了什么?音乐文件显然位于SD卡上,但查询却看不到它们。如果我将内容URI更改为INTERNAL_CONTENT_URI,则查询将查找内部存储中的文件(例如压电警报),而不是我的音乐文件。