列出Android设备上的所有文件类型?

时间:2011-03-14 12:57:47

标签: java android multithreading file

例如,我想检索内部和外部存储上的所有.mp3文件的列表。我还想要每个.mp3文件的路径(String)作为参考(将它们存储在List中?)我怎么能这样做?这是一个相对昂贵的操作吗?如果是这样,它应该放在一个线程上,当它运行时,向用户显示“正在加载”ProgressDialog

1 个答案:

答案 0 :(得分:6)

请找到以下功能。它将把所有mp3文件存储在外部和内部存储器中。

public void getAllSongs() {

    Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

    cursor = managedQuery(allsongsuri, STAR, selection, null, null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                song_name = cursor
                        .getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                int song_id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media._ID));

                String fullpath = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.DATA));
                fullsongpath.add(fullpath);

                album_name = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                int album_id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

                artist_name = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ARTIST));
                int artist_id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));


            } while (cursor.moveToNext());

        }
        cursor.close();
        db.closeDatabase();
    }
}