Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Context.getExternalMediaDir()?

The example in the Javadoc for Environment.getExternalStoragePublicDirectory() mentions Context.getExternalMediaDir() but this method isn't available in my Android API version and I can't find it documented anywhere in any version. Is this a typo or something that never made it to release?

void createExternalStoragePublicPicture() {
    // Create a path where we will place our picture in the user's
    // public pictures directory.  Note that you should be careful about
    // what you place here, since the user often manages these files.  For
    // pictures and other media owned by the application, consider
    // Context.getExternalMediaDir().
    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
like image 889
Jeff Axelrod Avatar asked Aug 10 '12 19:08

Jeff Axelrod


2 Answers

Nope, that's not a typo (anymore), it's a new method on the Android L Preview

Basically the geniuses at google, put a ".nomedia" file inside the /Android/data/ folder, and this makes any attempt of using a MediaScannerConnection scanFile() method fail inside your folder. So if you have an app that creates or downloads media you're probably doing some acrobatics now to scan that media and make it available to your provider, unless you have the new API, which we don't because every other phone is running a much older android...

For now our workaround has been to scan the files ourselves and insert them into the Content Providers that match it.

For images for example there's already a method you can use MediaStore.Images.Media.insertImage (ContentResolver cr, String imagePath, String name, String description)

we posted this solution in a discussion today and minutes later an engineer from Google told us about Context.getExternalMediaDir() which supposedly will give you a private path on the secondary external storage (aka, the Sd card) which is MediaScanning friendly.

"The new android.content.Context.getExternalMediaDirs() returns paths to these directories on all shared storage devices."

My guess (I haven't tested) is that the directory path will be something along the lines of

/storage/extSdCard/Android/media/

Whoever started managing the development decisions aroud the use of the SD Card is a real moron, the amont of bad judgment, poor decision making, and fuck up we've seen from Google-Android these last couple of weeks is starting remind me of Microsoft.

like image 186
Gubatron Avatar answered Oct 02 '22 17:10

Gubatron


I believe it is just incorrectly named; I think they meant to say "Context.getExternalFilesDir()". This may have been a typo, or the method may have been renamed later on.

Based on the explanation in the comments ("pictures and other media owned by the application"), it closely matches that of getExternalFilesDir(String type), which is "where the application can place persistent files it owns". Note that you can pass null to getExternalFilesDir, which returns the root directory, or specified to return a type of folder you're looking for.

I will say, I am speculating somewhat here. However, this seems to be the most likely explanation for this seemingly nonexistent method.

like image 36
Cat Avatar answered Oct 02 '22 15:10

Cat