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

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.

Hiding Images from Android's MediaScanner<p>I've been dealing with a nasty bug in <a href="https://play.google.com/store/apps/details?id=com.koushikdutta.backup">Helium</a> (<a href="https://plus.google.com/103583939320326217147/posts/LUBoUuetNA8">formerly Carbon</a>) since the initial release:</p><p><a href="/post/nomedia" title="Read more of Hiding Images from Android's MediaScanner">read more</a></p>