问题
In my app I want to set the ringtone
when I get an incoming call... How to open the SDCARD
and get audio files and list it.. How to get the URI
for the selected audio file
..
回答1:
MediaScanner finds music for you, populating the MediaStore database. Here's some code to look up a music entry:
final Uri uri = 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,
};
final String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
final Cursor cursor = getContentResolver().query(uri, cursor_cols, where, null, null);
cursor.moveToNext();
final String artist = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
final String album = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
final String track = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
doSomethingHere(artist, album, track);
The "data" field contains a handle you can use with MediaPlayer. while doing this media files scanning and indexing for the first time, scan it using a AsyncTask, but later when a intent is received do it using a service.
来源:https://stackoverflow.com/questions/5590628/i-want-get-audio-files-in-sd-card