Android 通過文件路徑直接修改文件名

今天遇到一個錄製視頻需要修改文件路徑的,因爲開始錄製視頻的時候已經生成文件名稱纔去錄製視頻,解決辦法就是錄製視頻結束後根據文件路勁修改文件名
下面直接貼代碼

/**
* 2 * 通過文件路徑直接修改文件名
* 3 *
* 4 * @param filePath 需要修改的文件的完整路徑
* 5 * @param newFileName 需要修改的文件的名稱
* 6 * @return
* 7
*/
public static String FixFileName(String filePath, String newFileName) {
File f = new File(filePath);
if (!f.exists()) { // 判斷原文件是否存在(防止文件名衝突)
return null;
}
newFileName = newFileName.trim();
if ("".equals(newFileName) || newFileName == null) // 文件名不能爲空
return null;
String newFilePath = null;
if (f.isDirectory()) { // 判斷是否爲文件夾
newFilePath = filePath.substring(0, filePath.lastIndexOf("/")) + “/” + newFileName;
} else {
newFilePath = filePath.substring(0, filePath.lastIndexOf("/")) + “/” + newFileName
+ filePath.substring(filePath.lastIndexOf("."));
}
File nf = new File(newFilePath);
try {
f.renameTo(nf); // 修改文件名
} catch (Exception err) {
err.printStackTrace();
return null;
}
return newFilePath;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章