Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getExternalFilesDir NOT creating new Directory

I am trying to CHECK to see if a Subdirectory Structure on the SD Card Exists;
If NOT then CREATE a new one.
That is of course AFTER my code verified that the SD Card is mounted and ready.

I created code that WORKED, and I successfully created the structure ON MY CELL PHONE'S SD Card, so I know my code was good. I then went to bed. The next day, I MOVED the working code from my TEST area to my WORKING area and now it won’t work!! :-( I even MOVED it Back to the test area and Still no workie!!

I'm guessing that at night the Android Gremlins did something to break the code!!

So...either the Android Gremlins did something OR perhaps (and this is a long shot) I might have forgotten something when I moved the code. Does anybody have any Gremlin repellent??

I’m pretty sure the code that WORKED and actually created the structure that I CAN NOW SEE on my phone was something like this:


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");  

On my Cell Phone, I can clearly see this structure. It is still there now!!

The next day, (i.e. AFTER a visit from the Android Gremlins) I attempted to MOVE this and other code FROM my Test area to a different section and after that, it no longer would CREATE a structure.

I step thru with the De-Bugger (i.e. Gremlin Killer) and here is what happens.

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  

IF…. I use the original name that I used the previous day "/MyDirectory/SubDir/xxx")
THEN the code indicates that the Directory DOES EXIST

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  

Now… as we ALL KNOW, programmers NEVER break their code and NEVER introduce errors into “known to be working code” so…. It MUST be Gremlins…. Right??


First Update

@ChuongPham commented that I may need to add the correct Permission in the Manifest file.

Since the code DID WORK, it means that I DID already enable this permission.
However, I decided to REMOVE this permission and see what ERROR it would throw, I thought it would not compile without the permission. I then ran the code and NO ERROR was thrown!! And YES, it still will NOT create a new file..

like image 227
user3239782 Avatar asked Dec 15 '22 23:12

user3239782


1 Answers

If you use m_Context.getExternalFilesDir("MyDirectory/SubDir") the custom directory is created automatically for you. Tested on API 19 or greater...

like image 116
Razec Luar Avatar answered Jan 03 '23 22:01

Razec Luar