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
434 views
in Technique[技术] by (71.8m points)

filter - Android songs fetching from SD card

I am fetching my songs from the SD card and putting him to the list view.

I am using this method. but its taking some time and if path is different I didn't get that data.

so , QUE Is there any helpfull script that display songs from all my sd card. If they are into directory/songs .

public ArrayList<HashMap<String, String>> getPlayList(){
        File home = new File(MEDIA_PATH);

        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
                song.put("songPath", file.getPath());

                // Adding each song to SongList
                songsList.add(song);
            }
        }
        // return songs list array
        return songsList;
    }


    class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".mp3") || name.endsWith(".MP3"));
        }
    }

Please give your comments on this .

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

I once used MediaStore for my music application, this is a very more efficient and correct way to retrieve data and then display it using a ListView. This will retrieve any music file stored in any folder on your SDCard.

    //your database elect statement 
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    //your projection statement 
    String[] projection = {
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.ALBUM_ID
    };
    //query 
    cursor = this.managedQuery(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            projection,
            selection,
            null,
            null);


    while(cursor.moveToNext()){
            songs.add(cursor.getString(0));
            songs.add(cursor.getString(1));
            songs.add(cursor.getString(2));
            songs.add(cursor.getString(3));
            songs.add(cursor.getString(4));
            songs.add(cursor.getString(5));
            album_id.add((long) cursor.getFloat(6));
    } 
    int a[]= new int[]{R.id.textView1 ,R.id.textView3};//, R.id.textview2};
    ListAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.items, cursor, new String[]{MediaStore.Audio.Media.TITLE,           MediaStore.Audio.Media.ARTIST/*, MediaStore.Audio.Media.DURATION*/} ,a);
            setListAdapter(adapter); 
}

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

...