首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在SD卡上写入.3gp文件

在SD卡上写入.3gp文件
EN

Stack Overflow用户
提问于 2013-05-06 14:57:31
回答 1查看 504关注 0票数 0

我有存储在SD卡中的.3gp音频文件。我想将该文件复制到SD卡的另一个文件夹中。我已经用谷歌搜索了很多关于它的内容,但没有得到任何有效的idea.Please帮助。如果到目前为止我尝试过的任何knows.The代码如下:

代码语言:javascript
复制
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()方法时,代码是:

代码语言:javascript
复制
 mFileFirst = new File(mFileName);//mFileName is the path of sd card where .3gp file is located
    save(mFileFirst);
EN

回答 1

Stack Overflow用户

发布于 2013-05-06 15:47:33

尝尝这个

代码语言:javascript
复制
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();
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16393569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档