如何通过将原始或资产文件夹中的音频保存在SD卡上或使用应用程序的安装地址来访问音频?



我正在尝试制作一个音乐专辑应用程序,其中包含一些歌曲并可以播放。我制作了播放器,但现在我被卡住了,因为我的播放器使用sd卡上特定地址的音乐,我需要复制音频或使用设备上安装的应用程序的资产(或行)文件夹(我不知道手机内存中是否有这个文件夹)。如果有更好的方法,请告诉我。

这是我的代码:

public class SongsManager {
    // SDCard Path
    final String MEDIA_PATH = new String("/sdcard/piano/");
    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;
    }

资产文件夹是可读的文件夹,您不能将文件从SD卡写入或存储到资产文件夹。在准备项目的最终构建之前,您可以先将一些Mp3文件添加到资产文件夹中,然后再将其复制到SD卡中。

现在,您可以从SD卡或资产访问这些文件。您只能使用一些RESTful POST web服务将Mp3歌曲从SD卡存储到服务器。

这是将文件从资产复制到sd卡的链接

如何从';资产';文件夹到SD卡?

希望它能帮助你

您可以检查文件夹是否存在:

File myFile = new File(myPath);
if (myFile.exists()) {
    // The folder exists so use the files in this folder
} else {
    // The folder does not exist so use the assets from the apps raw folder
}

<Additional>

您不想显示资产文件夹的地址(向用户列出应用程序的内部文件结构会很糟糕),但可以列出应用程序中原始目录中的文件,并允许用户从中进行选择。以下代码从原始文件夹的内容填充数组soundNames[]soundIds[]

Field[] fields = R.raw.class.getFields();
String[] soundNames = new String[fields.length];
int[] soundIds = new int[fields.length];
for (int i = 0; i < fields.length; i++) {
    soundNames[i] = fields[i].getName();
    soundIds[i] = getResources().getIdentifier(soundNames[i], null, null);
}

最新更新