1

I have read over the various other postings on this and have not yet found an answer that is working for me. They have been discussing the use of External Storage and I need to use 'default' (internal) storage.

I have a very simple routine in one of my Activity routines

String PATH = "/data/data/com.mydomain.myapplicationname/files";
SystemIOFile.MkDir(PATH);  // Ensure Recipient Path Exists

And then in my SystemIOFile class I have

static public Boolean MkDir(String directoryName) 
  {
    Boolean isDirectoryFound = false;
    File Dir = new File(directoryName);
    if(Dir.isDirectory())
    {
      isDirectoryFound = true;
    } else {
      Dir.mkdirs();
      if (Dir.isDirectory()) 
      {
         isDirectoryFound = true;
      }
  }
  Dir = null;
  return isDirectoryFound;
}

And in my Android.Manifest.xml I have

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

So it seems as though all of the pieces are in place for this to work.

BUT it is not working.
When I single step through the MKDir() routine it ALWAYS fails.
The

if (Dir.isDirectory())

always returns false
And the subsequent Dir.mkdirs() always returns false

What am I missing?

Jeeter
  • 5,887
  • 6
  • 44
  • 67
Dhugalmac
  • 554
  • 10
  • 20
  • possible duplicate of [Why is the mkdirs() method not working?](http://stackoverflow.com/questions/32001235/why-is-the-mkdirs-method-not-working) – Santiago Aug 17 '15 at 17:20

2 Answers2

2
"/data/data/com.mydomain.myapplicationname/files"

is not an external storage path. What you need to do is grab your application's allocated storage path.

Context c = /*get your context here*/;
File path = new File(c.getExternalFilesDir().getPath() + "/folder1/folder2/");
path.mkdirs();

If you need to access the app's internal filespace (provided the app accessing the space owns it) then you can use the Context's getFilesDir(). This will return the internal storage location of the app.

The code is essentially the same:

Context c = /*get your context here*/;
File path = new File(c.getFilesDir().getPath() + "/folder1/folder2/"); //this line changes
path.mkdirs();
Jeeter
  • 5,887
  • 6
  • 44
  • 67
  • Thank you for your reply. I am tasked with making mods to an existing Android app and that directory is what they are already using - so I am hesitant to change it. If it is not an External Storage area, how do I get the needed directory made? – Dhugalmac Aug 17 '15 at 17:07
  • I would suggest using `File` objects rather than `String`: `File path = new File(c.getExternalFilesDir(), "/folder1/folder2/")`; – Ted Hopp Aug 17 '15 at 17:08
  • @Dhugalmac I'm pretty sure it's "illegal" to modify anything not yours in internal storage (on external storage, I believe it's fair game) – Jeeter Aug 17 '15 at 17:11
  • @Jeeter - the Android app is our company's app. The former developer is no longer here. I don't want to break the parts that are already working - merely add the new functionality. Being a newbie to Android development, I want to understand how to work with the directory structure (and location) that is already in place. – Dhugalmac Aug 17 '15 at 17:24
  • @TedHopp - aren't I already using a File object in the MkDir() function? – Dhugalmac Aug 17 '15 at 17:27
  • @Dhugalmac he meant me, and I've updated the answer for using the internal files directory – Jeeter Aug 17 '15 at 17:30
2

This is a great question. First, it's better if you don't reference the /sdcard path directly. Use Environment.getExternalStorageDirectory().getPath() instead. Second, assuming you want to access your own app's files - you also don't want to reference the /data/data path directly because it can vary in multi-user scenarios. Use Context.getFilesDir().getPath() instead.

Implementing the above, we see that:

String PATH = "/data/data/com.mydomain.myapplicationname/files";
SystemIOFile.MkDir(PATH);

Returns false, whereas:

String PATH = Environment.getExternalStorageDirectory().getPath() 
        + getApplicationContext().getFilesDir().getPath();
SystemIOFile.MkDir(PATH);

Returns true.

Also note that you ignored the result of Dir.mkdirs() inside Boolean MkDir(String directoryName) .

Hope this helps.

Sahar Avr
  • 1,168
  • 9
  • 10
  • 1
    thanks for the reply. If,based on what the others have said above, the **/data/data/** directory is Not an External Storage location, wouldn't setting up the PATH based on **Environment.getExternalStorageDirectory().getPath()** be pointing me to the wrong location? – Dhugalmac Aug 17 '15 at 18:04
  • `/data/data` is not an external storage location, however `/storage/emulated/0/data/data` for example, is – Sahar Avr Aug 17 '15 at 18:35
  • thank you again for your reply. Since you say that **/data/data** is not an External Storage location and since that is the directory location currently being used in the application which I don't want to change, how do I go about making that directory (or ensuring that it has already been made)? – Dhugalmac Aug 17 '15 at 19:29
  • Actually `/data/data` is a part of your device's internal storage, and where all apps are installed to. You cannot browse it directly unless you have a rooted device. As for where it points to, it simply points to a part of your internal storage. – Sahar Avr Aug 17 '15 at 19:32
  • Once again thank you for your reply. I am not trying to Browse the directory. Instead I need to programmatically ensure that it exists so that I can save one or more downloaded files to it - for later retrieval by the app itself. – Dhugalmac Aug 17 '15 at 20:21
  • Generally, it _always_ exists. But if we want to be precise, we would declare the path like the way I showed above, create a File using `File file = new File(PATH);` and then call `file.exists()` to ensure that it does. – Sahar Avr Aug 17 '15 at 20:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87201/discussion-between-dhugalmac-and-sahar-avr). – Dhugalmac Aug 17 '15 at 20:55