2013-05-05 38 views
0

我有一个文件列表与一个longclicklistener,带来删除和重命名选项的上下文菜单。这些启动一个deleteDialog()或renameDialog()。这些调用delete()或rename()。删除工作,但重命名是给:为什么我得到FileNotFoundException?我可以看到这个位置的文件,所以我知道它存在

05-05 10:26:44.105: W/System.err(19017884): java.io.FileNotFoundException: Failed to rename file: /sdcard/My Webs/new/index.php 

甚至认为我可以看到这个文件在这个位置的文件系统上。

这里是我的警报代码:

void delete(File f) throws IOException { 
    if (f.isDirectory()) { 
     for (File c : f.listFiles()) 
      delete(c); 
    } 
    if (!f.delete()) 
     throw new FileNotFoundException("Failed to delete file: " + f); 
} 

void rename(File f, String newName) throws IOException { 
    File newFile = new File(newName); 

    f.renameTo(newFile); 

    if (!f.renameTo(newFile)) 
     throw new FileNotFoundException("Failed to rename file: " + f); 
} 

public void delDialog(int position) { 
    final int pos = position; 

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
    alertDialog.setIcon(R.drawable.remove); 
    alertDialog.setTitle(getString(R.string.delete)); 

    alertDialog.setButton("Delete", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 

      String selectedFileString = directoryEntries.get(pos).getText(); 
      File tmpFile = new File(currentDirectory.toString() 
        + selectedFileString); 

      try { 
       delete(tmpFile); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      directoryEntries.remove(pos); 
      itla.notifyDataSetChanged(); 

      currentFile = null; 
      changed = false; 
      return; 
     } 

    }); 
    alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      alertDialog.dismiss(); 
     } 
    }); 

    alertDialog.setMessage("Are you sure you want to delete this file?"); 
    alertDialog.show(); 
} 

public void renameDialog(int position) { 
    final int pos = position; 

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
    alertDialog.setIcon(R.drawable.renameicon); 
    alertDialog.setTitle(getString(R.string.rename)); 

    alertDialog.setButton("Rename", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 

      String selectedFileString = directoryEntries.get(pos).getText(); 
      File tmpFile = new File(currentDirectory.toString() 
        + selectedFileString); 

      try { 
       rename(tmpFile, "test.html"); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      itla.notifyDataSetChanged(); 

      return; 
     } 

    }); 
    alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      alertDialog.dismiss(); 
     } 
    }); 

    alertDialog.setMessage("Are you sure you want to rename this file?"); 
    alertDialog.show(); 
} 

public void Show_Context(Context context, String message, int position) { 
    final AlertDialog customDialog = new AlertDialog.Builder(this).create(); 
    LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext() 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = layoutInflater.inflate(R.layout.contextmenu, null); 
    final Button del = (Button) view.findViewById(R.id.delBtn); 
    final Button rename = (Button) view.findViewById(R.id.renameBtn); 
    final int pos = position; 
    customDialog.setView(del); 
    customDialog.setView(rename); 

    del.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      customDialog.dismiss(); 
      delDialog(pos); 
     } 
    }); 

    rename.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      customDialog.dismiss(); 
      renameDialog(pos); 
     } 
    }); 

    customDialog.setView(view); 
    customDialog.show(); 

} 

正如你可以看到代码为deleteDialog()和renameDialog()是一样的尚renameDialog()抛出FileNotFoundException异常

+0

你有没有在清单文件中加入这一行? '' – 2013-05-05 13:55:26

回答

1

有无你试图完全限定目的地的文件名?您目前正试图从currentDirectory.toString()+ selectedFileString重命名为“test.html”。

您可能想要尝试使用currentDirectory.toString()+“test.html”,否则您可能会遇到权限问题。

+0

谢谢!就是这样:) – RapsFan1981 2013-05-05 13:59:01

0

您可以拨打f.renameTo(newFile)两次!正常情况下一次,在if()条件下第二次。我想它已经第一次被重命名了,所以当你第二次做时,它会失败(或者因为文件不再有相同的名字或者因为它已经有了新的文件名)。

f.renameTo(newFile); 

if (!f.renameTo(newFile)) {...} 

尝试并移除第一个.f.renameTo()

另请注意,返回false的renameTo()可能有各种原因,不仅仅是文件找不到。当然,你会得到FileNotFoundException,因为你自己把它放在你的代码中:-)

相关问题