markdowneide 2013-04-28 05:19 采纳率: 0%
浏览 2318
已采纳

在一行中保存音频文件或者把 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
}
  • 写回答

1条回答

  • hxn_217 2013-04-28 08:22
    关注

    使用下面的代码试试:

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符