在android4.0的手機上直接創建某個文件的路徑一直報這個錯:open failed: ENOENT (No such file or directory)
在網上查了很多資料,沒找到解決方案,嘗試了多次終於找到解決辦法
如果在FileOutputStream創建一個流文件路徑時或者是對一個File文件路徑直接操作時,
可先創建文件的路徑,然后在創建文件名就不會在報該錯誤
以下是解決方案:
1 public static File getFilePath(String filePath, 2 String fileName) { 3 File file = null; 4 makeRootDirectory(filePath); 5 try { 6 file = new File(filePath + fileName); 7 } catch (Exception e) { 8 // TODO Auto-generated catch block 9 e.printStackTrace(); 10 } 11 return file; 12 } 13 14 public static void makeRootDirectory(String filePath) { 15 File file = null; 16 try { 17 file = new File(filePath); 18 if (!file.exists()) { 19 file.mkdir(); 20 } 21 } catch (Exception e) { 22 23 } 24 }