在 assets 文件夾中有一個 audio 文件,我需要把相同的文件保存到 sdcard 中,如何實現?我用下面的代碼來保存文件:
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
}
使用下面的代碼試試:
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();
轉換為 byte 數組後,把下面的代碼放到 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
}