i want get audio files in sd card

前提是你 提交于 2019-11-28 06:10:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!