problem with managed query
I'm trying to write application, which lists all mp3 files from sd card.
String[] columns = new String[] {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.TITLE_KEY,
MediaStore.Audio.Media.D开发者_C百科ATA,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ARTIST_ID,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.TRACK
};
String uristr_t = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.getPath();
Cursor musicCursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
columns, null, null, null);
if(musicCursor != null){
mSongs = new ArrayList<SongInfo>( musicCursor.getCount());
boolean isMediaFilesExist = musicCursor.moveToFirst();
...
There are some mp3-files on the sd card but musicCursor.GetCount() returns 0. I suppose that path is incorrect. EXTERNAL_CONTENT_URI is content://media/external/audio/media
and its path is:
/external/audio/media
but my sd card's path is:
/mnt/sdcard
And I've tried to set Uri manually:
String uriStr = "content://" + Environment.getExternalStorageDirectory().getPath();
Uri sdCardUri = Uri.parse(uriStr);
Cursor musicCursor = managedQuery(sdCardUri, columns, null, null, null);
In that case Uri is content:///mnt/sdcard
, but musicCursor is null after managedQuery. Maybe format of uriStr is incorrect and Uri initialization is wrong?
Thanks!
Your code is correct. The path /external/audio/media is the content provider path not the path int the actual filesystem. Are you sure you have MP3 files on the card? Check in the music app you can see files. If you can't in there, I sometimes find (especially on the emulator) that the indexing doesn't always run. Force the SD to get reindexed of all media by unmounting the SD card and remounting (Settings/SD card & phone storage/Unmount SD card).
精彩评论