所以在我用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
答案 0 :(得分:0)
根据File参考。许多失败是可能的。一些更可能的失败包括:
你可以逐一仔细检查吗?我想你可以知道发生了什么。
答案 1 :(得分:0)
首先检查文件是否存在: 如果它仍然无法工作,则必须手动完成
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();
}
}
答案 2 :(得分:0)
我明白了!问题出在这一行:
File directory = new File (externalStoragePath);
我把它更改为:
File directory = new File (externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/");
因为externalStoragePath返回整个路径(包括文件名)