0

我有存储在 SD 卡中的 .3gp 音频文件。我想将该文件复制到 sd 卡的另一个文件夹中。我已经用谷歌搜索了很多,但没有任何工作想法。如果有人知道,请帮助我。我到目前为止尝试过的代码如下:

private void save(File file_save) {

    String file_path = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/RecordedAudio";
    File file_dir = new File(file_path);
    if (file_dir.exists()) {
        file_dir.delete();
    }
    file_dir.mkdirs();
    File file_audio = new File(file_dir, "audio"
            + System.currentTimeMillis() + ".3gp");
    try {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(bos);
        out.writeObject(file_save);
        out.close();
        FileOutputStream fos = new FileOutputStream(file_audio);

        byte[] buffer = bos.toByteArray();
        fos.write(buffer);

        fos.flush();
        fos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

这总是创建一个大小为 100 的新文件。在此先感谢...当我调用此 save() 方法时的代码是:

 mFileFirst = new File(mFileName);//mFileName is the path of sd card where .3gp file is located
    save(mFileFirst);
4

1 回答 1

0

尝试这个

private void save(File file_save) {
    String file_path = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/RecordedAudio";
    File file_dir = new File(file_path);
    if (file_dir.exists()) {
        file_dir.delete();
    }
    file_dir.mkdirs();
    File file_audio = new File(file_dir, "audio"
            + System.currentTimeMillis() + ".3gp");
    try {

        FileInputStream fis = new FileInputStream(file_save);
        FileOutputStream fos = new FileOutputStream(file_audio);

        byte[] buf = new byte[1024];
        int len;
        int total = 0;
        while ((len = fis.read(buf)) > 0) {
            total += len;
            fos.write(buf, 0, len);
            // Flush the stream once every so often
            if (total > (20 * 1024)) {
                fos.flush();
            }
        }
        fos.flush();
        fis.close();
        fos.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
于 2013-05-06T07:47:32.563 回答