首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileOutputStream错误异常

FileOutputStream错误异常
EN

Stack Overflow用户
提问于 2014-02-16 20:32:38
回答 2查看 1.1K关注 0票数 0

您好,我在save = new FileOutputStream(path +filename)中得到一个错误;我不知道该怎么做,是媒体应用程序,此函数假定应用程序应将歌曲保存为铃声

代码语言:javascript
复制
 public boolean saveas(int type, int position) {
    Log.i("ramiro", "entro saveas");
    byte[] buffer = null;
    InputStream fIn = getBaseContext().getResources().openRawResource(
            s1[position]);
    int size = 0;

    try {
        size = fIn.available();
        buffer = new byte[size];
        fIn.read(buffer);
        fIn.close();
    } catch (IOException e) {
        Log.i("ramiro", "error1 try");
        return false;
    }

    String path = Environment.getExternalStorageDirectory().getPath()
            + "/media/audio/ringtones/";

    String filename = title[position];
    Log.i("ramiro", "filename: " + filename);

    boolean exists = (new File(path)).exists();
    if (!exists) {
        new File(path).mkdirs();
    }

    FileOutputStream save;
    try {
        save = new FileOutputStream(path + filename); //error
        save.write(buffer);
        save.flush();
        Log.i("ramiro", "paso flush");
        save.close();
    } catch (FileNotFoundException e) {
        Log.i("ramiro", "try2 filenotfoundexception");
        return false;
    } catch (IOException e) {
        Log.i("ramiro", "try2 ioexception");
        return false;
    }
EN

回答 2

Stack Overflow用户

发布于 2014-02-16 20:41:10

替换这个

代码语言:javascript
复制
FileOutputStream save;

使用

代码语言:javascript
复制
FileOutputStream save = null;
票数 0
EN

Stack Overflow用户

发布于 2014-02-16 21:32:11

以下是您的问题:

  • 你的缓冲区大小是0,该如何工作呢?
  • 即使你增加了你的缓冲区大小,你也只能读取一个缓冲区。你需要一个循环。

此外,如果您的代码在添加了//错误注释的地方崩溃:

  • 确保标题不为空
  • 确保您具有在外部存储上写入的权限
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21811137

复制
相关文章

相似问题

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