2013-08-01 49 views
0

因此,我用MediaRecorder录制某些内容并将其放置在Android设备中的某处。我如何重命名该文件?这是最接近我的解决方案。点击按钮后,没有任何反应。如何重命名现有文件?

public void nameAlert() { 
    AlertDialog.Builder nameAlert = new AlertDialog.Builder(this); 
    nameAlert.setMessage("Name of your recorded file:"); 
    final EditText input = new EditText(this); 
    nameAlert.setView(input); 

    nameAlert.setPositiveButton("Enter", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface arg0, int arg1) { 
      newFileName = input.getText(); 
      String currentFileName = externalStoragePath; 
      currentFileName = currentFileName.substring(1); 
      Log.i(storagePath, currentFileName); 

      File directory = new File (externalStoragePath); 
      File from = new File (directory, currentFileName); 
      File to = new File (directory, newFileName + ".mp3"); 
      from.renameTo(to); 
     } 
    }); 
    nameAlert.show(); 

此外,这可能是相关的。

externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath(); 

日志:

08-02 02:04:03.623: I/(14043): storage/emulated/0 
+1

你的代码是非常简单,只需它可以得到。 – FabianCook

+0

@SmartLemon有什么想法可能是错误的,但? – Pizzret

+0

Wheres onclick的代码? – FabianCook

回答

0

我想通了!问题是在这条线:

File directory = new File (externalStoragePath); 

我改成了这样:

File directory = new File (externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/"); 

因为externalStoragePath返回整个路径(包括文件名)

0

根据File参考。许多失败是可能的。一些更可能的故障包括:

  1. 对包含源路径和目标路径的目录需要写入权限。
  2. 对于两条路径的所有父母都需要搜索许可。
  3. 两条路径都在同一个挂载点上。在Android上,当尝试在内部存储和SD卡之间进行复制时,应用程序最有可能遇到此限制。 请注意,此方法在失败时不会抛出IOException。呼叫者必须检查返回值。

你可以仔细检查一个接一个吗?我想你可以找出发生了什么事。

+0

只是想通了,没有那些失败,检查我的答案,无论如何谢谢! – Pizzret

0

首先检查,如果该文件excisting: 如果它仍然不工作,它必须manualy做

if(to.exists()) throw new java.IOException("file exists"); 
    if (from.renameTo(to)) { 
     //success 
    }else{ 
     to.createNewFile(); 
     FileChannel FCfrom = null; 
     FileChannel FCto = null; 
     try { 
      FCfrom = new FileInputStream(from).getChannel(); 
      FCto = new FileOutputStream(to).getChannel(); 
      long count = 0; 
      long size = source.size();    
      while((count += destination.transferFrom(source, count, size-count))<size); 
     }finally { 
      if(FCto != null){ 
       FCto.close(); 
       FCfrom.close(); 
       from.delete(); 
     } 
    } 
+0

对不起,但我只是想了一秒钟前:)谢谢反正队友! – Pizzret