views:

59

answers:

2

At some points while running my android application, I need to create a directory on the sd card, for a small number of users this fails and I can't figure out the reason for it...

(I've found similar problems caused by the WRITE_EXTERNAL_STORAGE permission missing, it's there and it works for almost all users so I don't think this is reason)

When mkdirs returns false I crash the program and log the following java.io.File properties, starting at the directory I want to create, then recursive printing properties of the parent directory and so on...

/sdcard/MyDirectory/Dir1/Dir2 (exists: false, canWrite: false, isDirectory: false, isFile: false);
/sdcard/MyDirectory/Dir1 (exists: true, canWrite: true, isDirectory: true, isFile: false);
/sdcard/MyDirectory (exists: true, canWrite: true, isDirectory: true, isFile: false);
/sdcard (exists: true, canWrite: true, isDirectory: true, isFile: false);
/ (exists: true, canWrite: false, isDirectory: true, isFile: false);

The strange thing is that the parent directory is writable (canWrite=true), I can't print the execute file permission but from what I've read, write is what you need when creating directories...

What I've looked at so far is the WRITE_EXTERNAL_STORAGE permission, if the sd card is full and if the sdcard is mounted read only:

ExternalStorageState = mounted
AvailableSpace = 6467747840b

Also relevant I think, I've only got reports from users on Android 1.6 for this problem.. The devices vary but all of them are running 1.6..

Solutions, suggestions, ideas, all is welcome...

Thanks for any help...

Edit:

The actual code is a bit hard to post because it is located in a number of different classes and a settings file, what it comes down to is this:

File directory = new File(Environment.getExternalStorageDirectory(), "MyDirectory/Dir1/Dir2");
if (directory.exists() == false)
{
    directory.mkdirs();
    if (directory.exists() == false)
    {
        ExceptionHandler.fatal("failed to create " + directory + ", info : " + printRecursiveInfo(directory));
    }
}

where printRecursiveInfo gives me the info I posted above with canWrite etc...

A: 

It is common when you don't have a permission in your manifest.

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

As for me it was the only wrong thing.

Vladimir Ivanov
I only have this problem for a very small number of users and my AndroidManifest.xml has the WRITE_EXTERNAL_STORAGE permission inside the manifest tag so I'm sorry to say this is not the reason..
Danny
A: 

It is possible that these users have an SD card that is corrupt and thus mounted read-only. If possible, you should check with them to see if anything else can write files to it. Given that you have the WRITE_EXTERNAL_STORAGE permission, you should have no trouble making modifications to the SD card (and the permissions of everything in the SD card is world read/write for such apps, so nothing can mess with file permissions to cause them trouble either).

hackbod