将raw或assets文件夹中的音频文件保存到sdcard android

时间:2012-02-29 05:22:21

标签: android audio android-file

大家好我在资源文件夹中有一个音频文件,我需要将相同的文件保存到SD卡上。

如何实现这一点。

以下是我用来保存文件的代码

String filename = "filename.txt";
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream fos;
byte[] data = new String("data to write to file").getBytes();
try {
    fos = new FileOutputStream(file);
    fos.write(data);
    fos.flush();
    fos.close();
} catch (FileNotFoundException e) {
    // handle exception
} catch (IOException e) {
    // handle exception
}

请帮助

1 个答案:

答案 0 :(得分:2)

试试这个

AssetManager mngr = getAssets();
InputStream path = mngr.open("music/music1.mp3"); 

                    BufferedInputStream bis = new BufferedInputStream(path,1024);

                    //get the bytes one by one
                    int current = 0;

                    while ((current = bis.read()) != -1) {

                        baf.append((byte) current);
                    }
}
byte[] bitmapdata  = baf.toByteArray();

转换为字节数组后,将其复制到sdcard中,如下所示

File file = new File(Environment.getExternalStorageDirectory(), music1.mp3);
FileOutputStream fos;

try {
    fos = new FileOutputStream(file);
    fos.write(bitmapdata  );
    fos.flush();
    fos.close();
} catch (FileNotFoundException e) {
    // handle exception
} catch (IOException e) {
    // handle exception
}