Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 649
Graham Borland Avatar asked Mar 11 '26 19:03

Graham Borland


1 Answers

“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.

like image 76
Martin Avatar answered Mar 13 '26 11:03

Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!