我想要检查SD卡上的子目录结构是否存在;
如果不存在,则创建一个新的。
当然,在我的代码验证SD卡已经挂载且准备好之后。
我编写了代码,它可以工作,并且成功地在我的手机SD卡上创建了结构,因此我知道我的代码是好的。然后我去睡觉了。第二天,我把工作代码从测试区域移动到工作区域,现在它不起作用了!! :-( 我甚至将其移回测试区域,仍然无法工作!!
我猜夜间安卓小精灵做了些破坏代码的事情!
所以……要么是安卓小精灵做了些事情,要么(这很不可能)在我移动代码时我可能忘记了某些东西。有人有小精灵驱逐剂吗?
我非常确定可以工作并实际上创建了我现在可以在手机上看到的结构的代码类似于:
String strPathName = m_Context.getExternalFilesDir (null) .toString ();
// This DOES give the Correct Path on the SD Card
File dir;
dir = new File (m_Context.getExternalFilesDir (null) + "/MyDirectory");
dir = new File (m_Context.getExternalFilesDir (null) + "/MyDirectory/SubDir");
dir = new File (m_Context.getExternalFilesDir (null) + "/MyDirectory/SubDir/xxx");
我可以清楚地看到这个结构在我的手机上。它现在还在!!
第二天(即 Android Gremlins 来访之后),我试图将这个和其他代码从测试区域移动到另一个部分,但之后,它就不能再创建一个结构了。
我使用 De-Bugger(即 Gremlin Killer)进行步进调试,以下是发生的情况。
File dir;
boolean bDirExists;
dir = new File (m_Context.getExternalFilesDir (null) + "/NewDirectory");
bDirExists = (dir.exists () && dir.isDirectory () ); // FALSE
dir = new File (m_Context.getExternalFilesDir (null) + "/NewDirectory/SubDir");
bDirExists = (dir.exists () && dir.isDirectory () ); // FALSE
bDirExists = dir.exists (); // FALSE
bDirExists = dir.isDirectory (); // FALSE
dir = new File (m_Context.getExternalFilesDir (null) + "/NewDirectory/SubDir/xxx");
bDirExists = (dir.exists () && dir.isDirectory () ); // FALSE
如果……我使用了前一天使用的原始名称 "/MyDirectory/SubDir/xxx"
那么,代码表明该目录存在。
dir = new File (m_Context.getExternalFilesDir (null) + "/MyDirectory");
bDirExists = (dir.exists () && dir.isDirectory () ); // TRUE
dir = new File (m_Context.getExternalFilesDir (null) + "/MyDirectory/SubDir");
bDirExists = (dir.exists () && dir.isDirectory () ); // TRUE
bDirExists = dir.exists (); // TRUE
bDirExists = dir.isDirectory (); // TRUE
现在...正如我们都知道的那样,程序员永远不会破坏自己的代码,也不会在“已知可用的代码”中引入错误,所以...一定是小精灵造成的...对吗?
第一个更新
@ChuongPham评论说我可能需要在清单文件中添加正确的权限。
由于代码确实可以工作,这意味着我已经启用了此权限。
然而,我决定删除此权限并查看它会产生什么错误,我认为没有权限就无法编译。 然后我运行了代码,没有出现任何错误!! 是的,它仍然无法创建新文件...