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 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable