Alxandr
Alxandr

Reputation: 12431

Android file-creation fails

I use the following code to create a folder "mymir" and a file ".nomedia" (in the mymir-folder) on the sdcard of an android unit. However, somehow it fails with the exception that the folder the ".nomedia"-file is to be placed in dosn't exist. Here's the code:

private String EnsureRootDir() throws IOException
{
    File sdcard = Environment.getExternalStorageDirectory();
    File mymirFolder = new File(sdcard.getAbsolutePath() + "/mymir/");
    if(!mymirFolder.exists())
    {
        File noMedia = new File(mymirFolder.getAbsolutePath() + "/.nomedia");
        noMedia.mkdirs();
        noMedia.createNewFile();
    }
    return mymirFolder.getAbsolutePath();
}

Upvotes: 4

Views: 5142

Answers (1)

yanchenko
yanchenko

Reputation: 57206

I SD really there?

Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) == true

If targeting 1.6+, have you declared

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

?
The exact Exception could help.

Upvotes: 3

Related Questions