如何更改资源文件夹的路径?

时间:2013-06-01 00:15:06

标签: java android

我正在开发一个音频播放器,从Sdcard读取所有mp3文件然后播放它,现在我需要改变它的路径,我想让它读取资源文件夹中的mp3文件,怎么办呢?

这是代码

public class SongsManager {
// SDCard Path
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"));
    }
}

}

2 个答案:

答案 0 :(得分:0)

将其写入数据目录:Android - Creating a folder in the data/data/pkg/files directory

如果我记得它是/ data / data / your_package /(您可以在Eclipse的DDMS透视图中看到它)

答案 1 :(得分:0)

试用此代码:

AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor());
player.prepare();
player.start();