2014-06-24 88 views
-3

我需要在Java中使用ororid創建新文件。我不喜歡這樣寫道:在android中設置新文件名的奇怪行爲

public static File getAbosoluteFile(String relativePath, Context context) { 
     if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { 
      return new File(context.getExternalFilesDir(null), "AE " +".jpg"); 
     } else { 
      Toast.makeText(context, "internal", Toast.LENGTH_SHORT).show(); 
      return new File(context.getFilesDir(), "AE" +".jpg"); 
     } 
    } 

但是當我把它像這樣 - 它工作正常,但是當我使用字符串從方法更改名稱,例如:

public static String getCurrentDate() 
{ 
    String returnDate = null; 
    Calendar currentDate = Calendar.getInstance(); 
    int minute = currentDate.get(Calendar.MINUTE); 
    String editedMinute; 
    if (minute<10) 
     editedMinute = "0" + Integer.toString(minute); 
    else 
     editedMinute = Integer.toString(minute); 
    returnDate= (Integer.toString((currentDate.get(Calendar.MONTH) + 1)) + "/" +Integer.toString(currentDate.get(Calendar.DAY_OF_MONTH)) + "/" +Integer.toString(currentDate.get(Calendar.YEAR)) + " " + 
      Integer.toString(currentDate.get(Calendar.HOUR_OF_DAY)) + ":" + editedMinute); 
    return returnDate; 

} 

因此,即使,當我使用return new File(context.getExternalFilesDir(null), "12/12/12 " +".jpg"); 該文件是不會創建。我試圖在名稱中使用篩選 - 思想原因在於此,但結果相同。 Java的new File(File dir, String name)是如此依賴於名稱格式?或者是什麼原因?

+1

我什麼也不懂 –

+0

在文件路徑中不能有「:」。還有其他人物也被禁止。 LogCat會告訴你!你可以使用「/」,但是你引入了子目錄。 – greenapps

+0

爲什麼濫用?我添加了描述。 –

回答

0

您的文件名中不能使用/。它用作文件分隔符。

對於eg.If你有一個在你的SD卡,並在其命名爲App文件夾,名爲Sub子文件夾的路徑是一樣的東西mnt/sdcard/App/Submnt/sdcard僅僅是例如,它是不固定的)

這樣你就可以看看爲什麼您不能在文件名中使用/。使用_代替/

+0

謝謝,這很有幫助。 –