Android - .nomedia not working for images

Waypoint picture Waypoint · May 22, 2012 · Viewed 39.1k times · Source

I have images, which are read from sdcard only by particular application so I want to hide it from image gallery. I have put .nomedia file in it, but this file is ignored, images are still showing in Gallery. I have put it with "." in the beginning. Still not working. Any ideas?

Answer

Darshan Rivka Whittle picture Darshan Rivka Whittle · Jun 7, 2012

I'm guessing you're using Android 4.0, as .nomedia functioned properly until then. The AOSP bug report for the issue explains:

MediaScanner on Android 4.0 fails to forget already-indexed files when it encounters a .nomedia file, but it does honor it if it's present on the first pass. So the workaround is to simply rename the directory. Of course you can change the name back after the next scan, if you like.

Update: I suppose the complete solution would be to actually force a media scan after renaming the directory. I haven't tried it, but something like this should do the trick:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" +
    Environment.getExternalStorageDirectory())));