Android Finding Files On The Sdcard
I'm working on a MP3 application in which I'd like to index the files on my SDCard. What is the best way to do it? My Idea. Search for files when the application is started for the
Solution 1:
Agree with Chris, MediaScanner finds music for you, populating the MediaStore database. Here's some code to look up a music entry:
finalUriuri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
final String[] cursor_cols = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE,
};
finalStringwhere= MediaStore.Audio.Media.IS_MUSIC + "=1";
finalCursorcursor= getContentResolver().query(uri, cursor_cols, where, null, null);
cursor.moveToNext();
finalStringartist= cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
finalStringalbum= cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
finalStringtrack= cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
doSomethingInteresting(artist, album, track);
The "data" field contains a handle you can use with MediaPlayer.
Solution 2:
To avoid the ANR you need to do the search outside the UI thread. As SD cards can be big, you probably want to do it in a service rather than in your foreground activity so that the user can use their device for other things while the search is ongoing.
But android already finds and indexes supported media files, so you should see if you can leverage the built-in MediaScanner stuff.
Post a Comment for "Android Finding Files On The Sdcard"