2011-08-08 49 views
0

答:file.isDirectory()在Android的總是假的路徑

Needed the call to getExternalFilesDir(p);像這樣:

String p = thepathblah; 
File path=context.getExternalFilesDir(p); 

編輯編輯:

雖然我知道Environment.DIRECTORY_PICTURES返航只是Pictures/我想通這工作,因爲在android中,我認爲文件指針已經指向你的應用程序空間(就像在C#中)。所以在這:

String p = Environment.DIRECTORY_PICTURES + "/" + s.getClient().getFirstName()+s.getClient().getLastName() + 
"/" + s.getPackage().getName() + 
(mSession.getSessionDate().getMonth()+1) + 
mSession.getSessionDate().getDate() + 
    (mSession.getSessionDate().getYear()+1900); 

我認爲是得到完整的路徑,實際上我正在寫一個文件沒有問題。事實證明,雖然刪除單個文件(並加載它們),我需要它結束了一個更全面的路徑:

String p = Environment.DIRECTORY_PICTURES + "/" + s.getClient().getFirstName()+s.getClient().getLastName() + 
"/" + s.getPackage().getName() + 
(mSession.getSessionDate().getMonth()+1) + 
mSession.getSessionDate().getDate() + 
    (mSession.getSessionDate().getYear()+1900); 

File dir = new File("/sdcard/Android/data/com.software.oursoftware/files/"+p); 

不知道我是否可以認爲上面的鏈接是適用於所有的蜂窩設備或沒有,特別是/sdcard/Android/data/packagespace/files/

這是安全使用這個還是我必須做一些更動態的蜂窩設備?

編輯:這是我的小測試功能的代碼只寫東西到一個文件夾...

String p = Environment.DIRECTORY_PICTURES + "/" + s.getClient().getFirstName()+s.getClient().getLastName() + "/" + s.getPackage().getName() + (mSession.getSessionDate().getMonth()+1) + mSession.getSessionDate().getDate() + (mSession.getSessionDate().getYear()+1900); 
      File path = mContext.getExternalFilesDir(p); 
      File file = new File(path, "DemoPicture.jpg"); 

      try { 
       // Very simple code to copy a picture from the application's 
       // resource into the external file. Note that this code does 
       // no error checking, and assumes the picture is small (does not 
       // try to copy it in chunks). Note that if external storage is 
       // not currently mounted this will silently fail. 
       InputStream is = getResources().openRawResource(R.drawable.ic_contact_picture); 
       OutputStream os = new FileOutputStream(file); 
       byte[] data = new byte[is.available()]; 
       is.read(data); 
       os.write(data); 
       is.close(); 
       os.close(); 

       // Tell the media scanner about the new file so that it is 
       // immediately available to the user. 
       MediaScannerConnection.scanFile(mContext, 
         new String[] { file.toString() }, null, 
         new MediaScannerConnection.OnScanCompletedListener() { 
        public void onScanCompleted(String arg0, Uri arg1) { 
         Log.i("ExternalStorage", "Scanned " + arg0 + ":"); 
          Log.i("ExternalStorage", "-> uri=" + arg1); 

        } 
       }); 
      } catch (IOException e) { 
       // Unable to create file, likely because external storage is 
       // not currently mounted. 
       Log.w("ExternalStorage", "Error writing " + file, e); 
      } 

然後我嘗試刪除該文件夾的方式:

String p = Environment.DIRECTORY_PICTURES + "/" + firstName+lastName +"/" + pName+pDate; 
File dir=new File(p); 
deleteRecursive(dir); 

結果

Pictures/ShaneThomas/Portrait882011/

哪個可以寫一個文件,測試一下,但如果我試着說:

void deleteRecursive(File dir) 
{ 
    Log.d("DeleteRecursive", "DELETEPREVIOUS TOP" + dir.getPath()); 
    if (dir.isDirectory()) 
    { 
     String[] children = dir.list(); 
     for (int i = 0; i < children.length; i++) 
     { 
      File temp = new File(dir, children[i]); 
      if(temp.isDirectory()) 
      { 
       Log.d("DeleteRecursive", "Recursive Call" + temp.getPath()); 
       deleteRecursive(temp); 
      } 
      else 
      { 
       Log.d("DeleteRecursive", "Delete File" + temp.getPath()); 
       boolean b = temp.delete(); 
       if(b == false) 
       { 
        Log.d("DeleteRecursive", "DELETE FAIL"); 
       } 
      } 
     } 

     dir.delete(); 
    } 
} 

dir.isDirectory始終爲false!?我得到這個刪除文件/目錄代碼堆棧溢出,但我感到困惑,爲什麼它不工作?

,我確實有這一套:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+0

當你記錄dir.getAbsolutePath()時你會看到什麼? 它是目錄路徑嗎? –

+0

問題是什麼?下次更好地回答下面的問題,而不是無限次地編輯它。現在你的文本已經變得完全不可讀。 – ffonz

回答

1

我想你想File dir=new File(p)後立即添加

dir.mkdirs()。 mkdirs()是負責實際創建目錄的方法,而不是新的File()。

2

有用於File.isDirectory()返回false幾個原因:

  1. 的路徑指向文件(明顯),而不是目錄。
  2. 路徑無效(即不存在此類文件/目錄)。
  3. 沒有足夠的權限授予您的應用程序以確定路徑是否指向目錄。

一般來說,如果isDirectory()返回true,那麼您已經有了指向目錄的路徑。但是如果isDirectory()返回false,那麼它可能是或者可能不是一個目錄。

在您的特定情況下,最有可能的路徑不存在。您需要撥打dir.mkdirs()來創建路徑中的所有目錄。但是,由於您只需要遞歸刪除它們,因此在調用dir.mkdirs()之後就沒有必要刪除該目錄。

+0

它正在寫一個文件到目錄中,我可以將其放到我的模擬器中,然後在那裏看到它......所以多數民衆贊成在我身上。 – Codejoy

+0

父目錄的權限是什麼? – inazaruk

+0

hmmm它在我的應用程序文件空間下,我可以在shell的rmdir中找到一個空的目錄並且rm文件沒有問題。我編輯帖子以顯示我創建的目錄開始。 – Codejoy

1

確定它的回答,但有時因爲樣品reasone問題大火:許可:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

,如果您忘記這個權限,你總是會得到錯誤的結果。