FileOutputStream错误异常

时间:2014-02-16 12:32:38

标签: android fileoutputstream

我在save = new FileOutputStream(path + filename)中遇到错误; 我不知道该做什么,是媒体应用,这个功能假设应用程序应该将歌曲保存为铃声

 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;
    }

2 个答案:

答案 0 :(得分:0)

替换此

FileOutputStream save;

FileOutputStream save = null;

答案 1 :(得分:0)

以下是您的问题:

  • 你的缓冲区大小为0,该怎么办?
  • 即使提高缓冲区大小,也只能读取一个缓冲区。你需要一个循环。

此外,如果您的代码崩溃,您添加了//错误评论:

  • 确保标题不为空
  • 确保您具有在外部存储空间上书写的权限