Android - .nomedia not working for images

2019-01-17 15:28发布

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?

9条回答
Emotional °昔
2楼-- · 2019-01-17 16:19

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())));
}
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-17 16:20

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())));
查看更多
beautiful°
4楼-- · 2019-01-17 16:26

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.

查看更多
登录 后发表回答