将文件从我的 res/raw 文件夹下载(复制?)到默认的 Android 下载位置?

sha*_*han 3 android

我正在制作一个用于练习的音板,我想让用户能够res/raw在单击菜单项时下载声音(我已将其包含在应用程序的文件夹中),但我只能找到有关从互联网网址下载的信息,不是我已经包含在 apk 中的东西。

做这个的最好方式是什么?如果可能的话,我想让他们选择保存到 SD 卡。指出在文档中使用的正确类会很棒!我一直在谷歌搜索没有结果。

谢谢!

Jon*_*ica 5

尝试这样的事情:

public void saveResourceToFile() {
InputStream in = null;
FileOutputStream fout = null;
try {
    in = getResources().openRawResource(R.raw.test);
    String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
    String filename = "myfile.mp3"
    fout = new FileOutputStream(new File(downloadsDirectoryPath + "/"+filename));

    final byte data[] = new byte[1024];
    int count;
    while ((count = in.read(data, 0, 1024)) != -1) {
        fout.write(data, 0, count);
    }
} finally {
    if (in != null) {
        in.close();
    }
    if (fout != null) {
        fout.close();
    }
}
}
Run Code Online (Sandbox Code Playgroud)