我如何才能从Android上的SD卡获得所有音频文件?

ztigrdn8  于 2022-12-31  发布在  Android
关注(0)|答案(4)|浏览(184)

我想写一个类,从整个SD卡中获取所有的MP3文件。但实际上它只获取直接放在SD卡上的音频文件。所以它不搜索子文件夹。我想使用Mediastore,但我不把它放到数组列表中。

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;

import de.me.musicplayer.SongsManager.FileExtensionFilter;

import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;



public class SongsManager {

final String MEDIA_PATH = new String("/sdcard/");
private ArrayList<HashMap<String, String>> songsList = new         ArrayList<HashMap<String, String>>();

// Constructor
public SongsManager(){

}

/**
 * Function to read all mp3 files from sdcard
 * and store the details in ArrayList
 * */
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 to filter files which are having .mp3 extension
 * */
class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3") || name.endsWith(".MP3"));
    }
}
}
enxuqcxy

enxuqcxy1#

final String MEDIA_PATH = Environment.getExternalStorageDirectory()
        .getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";

// Constructor
public SongsManager() {

}

/**
 * Function to read all mp3 files and store the details in
 * ArrayList
 * */
public ArrayList<HashMap<String, String>> getPlayList() {
    System.out.println(MEDIA_PATH);
    if (MEDIA_PATH != null) {
        File home = new File(MEDIA_PATH);
        File[] listFiles = home.listFiles();
        if (listFiles != null && listFiles.length > 0) {
            for (File file : listFiles) {
                System.out.println(file.getAbsolutePath());
                if (file.isDirectory()) {
                    scanDirectory(file);
                } else {
                    addSongToList(file);
                }
            }
        }
    }
    // return songs list array
    return songsList;
}

private void scanDirectory(File directory) {
    if (directory != null) {
        File[] listFiles = directory.listFiles();
        if (listFiles != null && listFiles.length > 0) {
            for (File file : listFiles) {
                if (file.isDirectory()) {
                    scanDirectory(file);
                } else {
                    addSongToList(file);
                }

            }
        }
    }
}

private void addSongToList(File song) {
    if (song.getName().endsWith(mp3Pattern)) {
        HashMap<String, String> songMap = new HashMap<String, String>();
        songMap.put("songTitle",
                song.getName().substring(0, (song.getName().length() - 4)));
        songMap.put("songPath", song.getPath());

        // Adding each song to SongList
        songsList.add(songMap);
    }
}
lymgl2op

lymgl2op2#

您还可以使用 ContentResolver 类进行提取。
我已经这样做了,它的工作很好。

public void getSongListFromStorage() {

        // Retrieve song information
        ContentResolver musicResolver = getContentResolver();
        Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

        if (musicCursor != null && musicCursor.moveToFirst()) {

            // Get columns
            int titleColumn = musicCursor.getColumnIndex
                        (android.provider.MediaStore.Audio.Media.TITLE);
            int idColumn = musicCursor.getColumnIndex
                        (android.provider.MediaStore.Audio.Media._ID);
            int artistColumn = musicCursor.getColumnIndex
                        (android.provider.MediaStore.Audio.Media.ARTIST);

        // Add songs to the list
        do {
            long thisId = musicCursor.getLong(idColumn);
            String thisTitle = musicCursor.getString(titleColumn);
            String thisArtist = musicCursor.getString(artistColumn);
            songList.add(new Song(thisId, thisTitle, thisArtist));
        } while (musicCursor.moveToNext());

    }

}
c90pui9n

c90pui9n3#

这段代码将为您提供散列Map的arrayList,其内容是歌曲标题以及歌曲的绝对路径。

public ArrayList<HashMap<String, String>> getAudioList() {

    ArrayList<HashMap<String, String>> mSongsList = new ArrayList<HashMap<String, String>>();
    Cursor mCursor = getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaStore.Audio.Media.DISPLAY_NAME,
                    MediaStore.Audio.Media.DATA }, null, null, null);

    int count = mCursor.getCount();
    System.out.println("total no of songs are=" + count);
    HashMap<String, String> songMap;
    while (mCursor.moveToNext()) {
        songMap = new HashMap<String, String>();
        songMap.put("songTitle",mCursor.getString(mCursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)));
        songMap.put("songPath", mCursor.getString(mCursor
                .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
        mSongsList.add(songMap);
    }
    mCursor.close();
    return mSongsList;
}
ulmd4ohb

ulmd4ohb4#

private void getallaudio()
    {
        String[] STAR = { "*" };
        controller.audioWrapper.clear();
        Cursor audioCursor = ((Activity) cntx).managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, STAR, null, null, null);
        if (audioCursor != null) 
        {
            if (audioCursor.moveToFirst()) 
            {
                do 
                {
                    String path = audioCursor.getString(audioCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                    audioWrapper.add(new MediaWrapper(new File(path).getName(), path, "Audio",false));
//                    Log.i("Audio Path",path);
                }while (audioCursor.moveToNext());

            }
//            audioCursor.close();
        }
    }

相关问题