Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
593 views
in Technique[技术] by (71.8m points)

filter - How to find mp3 files from sdcard in android programatically

I am new to android programming so can anyone please help me to find all .mp3 files in my android device.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should use MediaStore. Here is an example code i'm using for something similar:

private static ArrayList<SongModel> LoadSongsFromCard() {
    ArrayList<SongModel> songs = new ArrayList<SongModel>();

    // Filter only mp3s, only those marked by the MediaStore to be music and longer than 1 minute
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"
            + " AND " + MediaStore.Audio.Media.MIME_TYPE + "= 'audio/mpeg'"
            + " AND " + MediaStore.Audio.Media.DURATION + " > 60000";

    final String[] projection = new String[] {
        MediaStore.Audio.Media._ID,             //0
        MediaStore.Audio.Media.TITLE,           //1
        MediaStore.Audio.Media.ARTIST,          //2
        MediaStore.Audio.Media.DATA,            //3
        MediaStore.Audio.Media.DISPLAY_NAME
    };

    final String sortOrder = MediaStore.Audio.AudioColumns.TITLE
            + " COLLATE LOCALIZED ASC";

    Cursor cursor = null;

    try {
        // the uri of the table that we want to query
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; //getContentUriForPath("");
        // query the db
        cursor = _context.getContentResolver().query(uri,
                projection, selection, null, sortOrder);
        if (cursor != null) {
            cursor.moveToFirst();                       
            while (!cursor.isAfterLast()) { 
                //if (cursor.getString(3).contains("AwesomePlaylists")) {
                    SongModel GSC = new SongModel();
                    GSC.ID = cursor.getLong(0);
                    GSC.songTitle = cursor.getString(1);
                    GSC.songArtist = cursor.getString(2);
                    GSC.path = cursor.getString(3);

                    // This code assumes genre is stored in the first letter of the song file name
                    String genreCodeString = cursor.getString(4).substring(0, 1);
                    if (!genreCodeString.isEmpty()) {
                        try {
                            GSC.genre = Short.parseShort(genreCodeString);
                        } catch (NumberFormatException ex) {
                            Random r = new Random();
                            GSC.genre = (short) r.nextInt(4);
                        } finally {
                            songs.add(GSC);
                        }
                    }
                //}
                cursor.moveToNext();
            }
        }
    } catch (Exception ex) {

    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return songs;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...