答: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"/>
當你記錄dir.getAbsolutePath()時你會看到什麼? 它是目錄路徑嗎? –
問題是什麼?下次更好地回答下面的問題,而不是無限次地編輯它。現在你的文本已經變得完全不可讀。 – ffonz