Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
42 lines (34 sloc) 2.46 KB

{{{ "title" : "Hiding Images from Android's MediaScanner", "date": "5-4-2013" }}}

I've been dealing with a nasty bug in Helium (formerly Carbon) since the initial release: The app icons that are saved to the SD Card during app backup pollute the user's Gallery. I tore my hair out trying to fix this, as this was the number one complaint about the app, including:

  • .nomedia file in all relevant directories. This does not work as of Android 4.0.
  • Naming the image as a hidden file by dot prefixing it, ie ".com.package.png"
  • Putting the hidden png file in a .nomedia directory.

Then the obvious solution hit me: look at the Android source code and see how the MediaScanner works, and figure out how to bypass it.

Digging through MediaScanner.java, I found that any file designated as "isNoMediaFile" would not be scanned. But, I didn't want to name the image.png files, .nomedia, as that is just hacky. Looking through the implementation of isNoMedia, I found that not just .nomedia files fall into this category...

// special case certain file names
// I use regionMatches() instead of substring() below
// to avoid memory allocation
int lastSlash = path.lastIndexOf('/');
if (lastSlash >= 0 && lastSlash + 2 < path.length()) {
    // ignore those ._* files created by MacOS
    if (path.regionMatches(lastSlash + 1, "._", 0, 2)) {
        return true;
    }

Well, apparently '._' prefixed files are also considered "isNoMediaFile"! This is there because mounting the external SD card on a Mac often results in the Mac creating '._' files in various directories. This is the Mac convention for hidden system files, and Android needs to ignore those files as well!

So, the fix was to change "file.png" to "._file.png". Though, one should also keep a .nomedia file anyways, as this is the recommended practice, and it also ensures backwards compatibility with older versions of Android.

Although I consider it a bug that the MediaScanner is scanning directories with .nomedia files in them, I am relieved and happy to find a workaround.