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 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题