Android special directories
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 car开发者_C百科d. 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?
“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 (Storage, "Android/FX-603P");
Dir.mkdirs ();
I used the “Android” base directory as it seem the new preferred way since SDK8.
精彩评论