问题描述
- 在一行中保存音频文件或者把 assets 文件夹保存到 sdcard 中
- 在 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(path1024); //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}
时间: 2024-08-18 11:25:35