2013-08-01 47 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