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?
-
4I don't know if this would actually work (since I don't know if Android looks into the data or just at the file extension) but have you tried changing the file extension to something that is entirely your own? Since you know what the files are you can load them yourself, but Android might overlook them? – Yevgeny Simkin Jun 01 '12 at 05:48
-
@Dr.Dredel +1, just removing the extension should do good. – AjOnFire Jun 01 '12 at 06:45
-
Can you post your code for creating the .nomedia file so we can verify it's correct? Otherwise, using Dr. Dredel's recommendation should work. Otherwise, I'd say look at this other answer http://stackoverflow.com/questions/2733576/android-file-creation-fails and make sure you have the correct permission. – Brayden Jun 01 '12 at 20:46
-
what device are you testing this on? I have not had problem with .nomedia file before. Are you sure that the file is named correctly. How are you adding this file? – Orlymee Jun 07 '12 at 11:19
9 Answers
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())));

- 32,989
- 7
- 91
- 109
-
2Note that rebooting phone also reindexes images [tested on Andro 4.1.2] – Grzegorz Dev Nov 07 '13 at 13:03
-
@GrzegorzDev how to check after rebooting? it is not working with ".nomedia" – Ando Masahashi Sep 22 '15 at 14:11
-
See http://android.stackexchange.com/questions/51784/can-i-trigger-a-media-scan-via-the-command-line – Ken Sharp Feb 19 '16 at 21:27
-
-
Note that “Intent.ACTION_MEDIA_MOUNTED” broadcast not work on Android 4.4+. – Veer Aug 17 '17 at 07:42
Well... I admit I'm only guessing, but since it got a +1 in the comment, I'll give it a whirl as an answer. Since this is your own app, change the image file's extension to something totally unique ".myimagefile" for instance, and see if that hides it from Android. Meanwhile you know what files to load into your own app, and how to save them.

- 27,946
- 39
- 137
- 236
-
2`MediaScanner` does use file extensions to determine file type, so renaming every file would work, but simply renaming the directory (after adding `.nomedia`) is much simpler. – Darshan Rivka Whittle Jun 08 '12 at 07:22
-
as I said, I was just going by what the OP described as his problem and guessing at what a possible solution might be. It sounds like .nomedia isn't a perfect solution, going by the other answer, but I can see how doing a file renaming scheme might be more of a hassle. – Yevgeny Simkin Jun 09 '12 at 08:16
The .nomedia bug is right and you CAN add it to a preexisting image folder.
You can force the gallery to re-index after you created the .nomedia file by renaming the folder, run gallery (the "hidden folder should now be missing), and rename the folder back to what you want. It should remain hidden.
I just did it again to ensure it works on my phone. Android 4.0.4.

- 61
- 1
- 1
Here's something I found myself:
However, a little alteration to that method:
- Download and install SDRescan app
- Clear data for Gallery app
- Clear data for Media Storage app
- Open SDRescan app downloaded from step 1
SDCard should be re-scanned and all directories containing .nomedia files should be ignored at this point.

- 71
- 7
-
This is what worked for me. It's simple. In my case, I could not have renamed the folder because it was the Whatsapp Images and Whatsapp Videos other Whatsapp media folders which I didn't want to show up in the Gallery. Renaming the folder would probably make whatsapp app lose the data as well. That's why, this method is the best and safest method. – finitenessofinfinity Nov 07 '13 at 12:54
In my memory card I have thousands of photos that overwhelm the media-scanner. Then in the root folder on my memory card, renamed from "RootFolder" to ".RootFolder"
Became the hidden directory and not use more .nomedia
Try this.

- 170,088
- 45
- 397
- 571

- 6,490
- 2
- 35
- 37
The .nomedia
file is preventive, not curative (in Android 4.0), and it is a bug.
If you had a .nomedia
file and images are already in it, Gallery will still search for images.
You need to create your .nomedia
file before puting images in it.
Edit: all folders starting with a dot are not scanned (because hidden).

- 17,833
- 13
- 59
- 112
-
"preventative, not curative" This is only true on Ice Cream Sandwich -- see my answer. – Darshan Rivka Whittle Jun 07 '12 at 12:37
With Media Storage 4.1.1 on my Samsung Galaxy Note 2, none of the tricks given above works fully, in this sense: directories containing .nomedia files are still scanned, which can drain the battery a lot (though the result is ignored in the gallery), and also directories with names starting with a '.' are still scanned :-(
I resorted to using the "Media Scanner Root" app, which allows to disable the crappy media scanner altogether. This is apparently achieved by something like
pm disable com.android.providers.media/com.android.providers.media.MediaScannerReceiver
BTW, disabling/freezing the Media Storage app itself has the bad effect that the selection and binding of ringtones and notifications does not work any more (what a wonderful design), but when only disabling its scanner component, one can still use, e.g., the ES File Explorer for selecting tones.

- 336
- 1
- 10
Gallery is not updated so often and some thumbnails are still on the view. For this u need to reboot the phone or programmatically update gallery, by following code.
/**
* This method is must to reflect changes on gallery, if some file is
* deleted or moved, <b> no updation is shown on gallery till phone is
* rebooted </b> or <i>till this method UpdateGallery(Activity activity) is
* called</i> UpdateGallery
*/
public static void UpdateGallery(Activity activity) {
activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
.parse("file://" + Environment.getExternalStorageDirectory())));
}

- 1,068
- 12
- 11
-
Rebooting the phone worked, at least on Android 7.1.2 (LineageOS), thanks a lot! – tuxayo Jul 26 '18 at 22:52
You might consider using the APK Expansion Zip Library (requires API level 5 or above) to read files that you will have installed to the SD card as part of a Zip archive. A description of this library can be found here:
http://developer.android.com/guide/market/expansion-files.html#ZipLib
Presumably, files stored in a ZIP archive on your SD card will not be displayed by the Gallery.
In addition, if you have images that your app reads from (but does not write to) the SD card, and that should only be available to your own app, and that could be delivered when your app is installed or updated, then it might make sense to take advantage of a new Google Play facility to incorporate them as part of your app when it is installed.
I'm guessing that you are storing your images to the SD card because they are too large to fit into your APK file (given its 50MB size limit, which is even smaller than that in practice for certain devices, due to buffering issues).
If so, then you might consider that Google Play, as of 3/5/2012, supports the attachment (to your app) of up to two "expansion files" of up to 2 Gigabytes each. The APK still has a 50MB maximum, but the expansion files raise the total storage available to over 4 GB. Those expansion files are served for you by Google, and they are maintained in a specific SD card folder that is specific to your app, and can be accessed using an API provided by the Downloader Library (requires API level 5 or above).
Here is Google's announcement of this change:
http://android-developers.blogspot.in/2012/03/android-apps-break-50mb-barrier.html
And here is a detailed description of the facility:
http://developer.android.com/guide/market/expansion-files.html
Note that while this facility is new, the libraries have been supplied with the Android SDK for use with earlier API levels. For example, on my installation, I find the API-8 versions here:
<Android SDK base folder>\extras\google\play_apk_expansion\
That same folder includes the abovementioned APK expansion Zip library in its zip_file sub-folder.
I have not personally used this facility yet myself, and so am describing this based solely on the cited references.

- 15,445
- 5
- 55
- 53