在SD卡上写.3gp文件

时间:2013-05-06 06:57:31

标签: android file-io android-external-storage

我有.3gp音频文件存储在SD卡中。我想将该文件复制到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);

1 个答案:

答案 0 :(得分: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();
    }

}