views:

175

answers:

1

I see that as of API Level 8, you can request a handle to special directories with Environment.getExternalStoragePublicDirectory. This takes a type such as DIRECTORY_MUSIC, DIRECTORY_PICTURES, etc.

I'm looking for an equivalent directory for other media types such as documents (Word docs, PDFs, etc.). My HTC Desire has a "/sdcard/My Documents/" folder which looks like exactly what I want, but it has no DIRECTORY_ specifier for use with the above method. Is this a standard Android directory which I can rely on?

If I can't use getExternalStoragePublicDirectory, I have to use getExternalStorageDirectory instead which just points me to the root of the SD card. I can explicitly check for the presence of "My Documents" under the root and use it if it's there.

This is what I have:

 // default document save location
 File defaultSaveDir = Environment.getExternalStorageDirectory();
 File myDocs         = new File(defaultSaveDir, "My Documents");
 if (myDocs.exists() && myDocs.isDirectory())
     defaultSaveDir = myDocs;

Is there a better way?

A: 

“My Documentes” is not there on my Nexus. Instead I have an “Documents” directory. So that won't help in a portable way.

With SDK 8 you should use getExternalFilesDir - but beware there is a catch. I wrote a little article in my Blog.

In the end I used:

final java.io.File Storage = android.os.Environment.getExternalStorageDirectory ();
final java.io.File Dir = new java.io.File (Storrage, "Android/FX-603P");
Dir.mkdirs ();

I used the “Android” base directory as it seem the new preferred way since SDK8.

Martin
Thanks, I can't use getExternalFilesDir for the reasons you mention. It's a pity that "My Documents" and "Documents" appear to be named differently on different devices.
Graham Borland
So it is a bug after all:http://groups.google.com/group/android-developers/browse_thread/thread/b68d40b1f13e12df/f3f37cc59d35617Scroll to the end. I wonder how long it will take until all devices are updated.
Martin