Android - .nomedia 不适用于图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10697276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:39:19  来源:igfitidea点击:

Android - .nomedia not working for images

androidimagemedia

提问by Waypoint

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?

我有图像,这些图像仅由特定应用程序从 SD 卡读取,因此我想将其从图库中隐藏。我已将 .nomedia 文件放入其中,但此文件被忽略,图像仍显示在图库中。我把它放在了“。” 在一开始的时候。还是行不通。有任何想法吗?

回答by Darshan Rivka Whittle

I'm guessing you're using Android 4.0, as .nomediafunctioned properly until then. The AOSP bug report for the issueexplains:

我猜您使用的是 Android 4.0,在此之前.nomedia功能正常。该问题的 AOSP错误报告解释了:

MediaScanneron Android 4.0 fails to forget already-indexed files when it encounters a .nomediafile, 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.

MediaScanner在 Android 4.0 上遇到文件时无法忘记已编入索引的.nomedia文件,但如果它在第一次通过时出现,它会尊重它。所以解决方法是简单地重命名目录。当然,如果您愿意,您可以在下次扫描后将名称改回。

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())));

回答by Yevgeny Simkin

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.

嗯...我承认我只是猜测,但由于它在评论中得到了 +1,我会试一试作为答案。由于这是您自己的应用程序,例如,将图像文件的扩展名更改为完全独特的“.myimagefile”,然后查看是否对 Android 隐藏了它。同时,您知道将哪些文件加载​​到您自己的应用程序中,以及如何保存它们。

回答by Isamu

The .nomedia bug is right and you CAN add it to a preexisting image folder.

.nomedia 错误是正确的,您可以将其添加到预先存在的图像文件夹中。

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.

在创建 .nomedia 文件后,您可以通过重命名文件夹来强制画廊重新索引,运行画廊(“隐藏的文件夹现在应该丢失),然后将文件夹重命名回您想​​要的名称。它应该保持隐藏状态。

I just did it again to ensure it works on my phone. Android 4.0.4.

我只是再次这样做以确保它可以在我的手机上运行。安卓 4.0.4。

回答by devo1929

Here's something I found myself:

这是我自己发现的东西:

http://www.transformerforums.com/forum/asus-transformer-faq/17765-solved-nomedia-doesnt-work-under-honeycomb-ics.html

http://www.transformerforums.com/forum/asus-transformer-faq/17765-solved-nomedia-doesnt-work-under-honeycomb-ics.html

However, a little alteration to that method:

但是,对该方法稍作改动:

  1. Download and install SDRescanapp
  2. Clear data for Gallery app
  3. Clear data for Media Storage app
  4. Open SDRescan app downloaded from step 1
  1. 下载并安装SDRescan应用程序
  2. 清除图库应用的数据
  3. 清除媒体存储应用程序的数据
  4. 打开从步骤 1 下载的 SDRescan 应用程序

SDCard should be re-scanned and all directories containing .nomedia files should be ignored at this point.

应重新扫描 SDCard,此时应忽略所有包含 .nomedia 文件的目录。

回答by Sergio Cabral

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"

在我的存储卡中,我有数以千计的照片,让媒体扫描仪不堪重负。然后在我的存储卡上的根文件夹中,从“RootFolder”重命名为“.RootFolder”

Became the hidden directory and not use more .nomedia

成为隐藏目录,不再使用更多 .nomedia

Try this.

尝试这个。

回答by dvo

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

在我的三星 Galaxy Note 2 上使用 Media Storage 4.1.1,上面给出的技巧都没有完全奏效,从这个意义上说:仍然扫描包含 .nomedia 文件的目录,这会消耗大量电池(尽管结果在画廊),以及名称以“.”开头的目录 仍然被扫描:-(
我求助于使用“媒体扫描仪根”应用程序,它允许完全禁用蹩脚的媒体扫描仪。这显然是通过类似的东西实现的

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.

顺便说一句,禁用/冻结媒体存储应用程序本身具有不良影响,铃声和通知的选择和绑定不再起作用(多么美妙的设计),但是当仅禁用其扫描仪组件时,仍然可以使用,例如, ES 文件资源管理器用于选择音调。

回答by taran mahal

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())));
}

回答by shkschneider

The .nomediafile is preventive, not curative (in Android 4.0), and it is a bug. If you had a .nomediafile and images are already in it, Gallery will still search for images.

.nomedia文件是预防性的,而不是治疗性的(在 Android 4.0 中),它是一个错误。如果您有一个.nomedia文件并且其中已有图像,Gallery 仍会搜索图像。

You need to create your .nomediafile beforeputing images in it.

您需要先创建您的.nomedia文件,然后再将图像放入其中。

Edit: all folders starting with a dot are not scanned (because hidden).

编辑:所有以点开头的文件夹都不会被扫描(因为隐藏)。

回答by Carl

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:

您可能会考虑使用 APK 扩展 Zip 库(需要 API 级别 5 或更高级别)来读取您将作为 Zip 存档的一部分安装到 SD 卡的文件。可以在此处找到此库的描述:

http://developer.android.com/guide/market/expansion-files.html#ZipLib

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.

据推测,存储在 SD 卡上的 ZIP 存档中的文件不会显示在图库中。

In addition, ifyou have images that your app readsfrom (but does not writeto) 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 partof your app when it is installed.

此外,如果您的应用程序从SD 卡读取(但不写入)图像,并且该图像应该只对您自己的应用程序可用,并且可以在您的应用程序安装或更新时交付,那么它可能在安装时利用新的 Google Play 工具将它们合并为应用程序的一部分是有意义的。

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

我猜您将图像存储到 SD 卡是因为它们太大而无法放入您的 APK 文件中(鉴于其 50MB 的大小限制,由于缓冲问题,这甚至比某些设备在实践中的大小还要小)。

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

如果是这样,那么您可能会认为,截至 2012 年 3 月 5 日,Google Play 支持最多两个“扩展文件”的附件(到您的应用程序),每个文件最多 2 GB。APK 仍然有 50MB 的最大值,但扩展文件将可用的总存储空间提高到 4GB 以上。这些扩展文件由 Google 为您提供,它们保存在特定于您的应用程序的特定 SD 卡文件夹中,并且可以使用下载程序库提供的 API 进行访问(需要 API 级别 5 或更高级别)。

Here is Google's announcement of this change:

这是谷歌关于这一变化的公告:

http://android-developers.blogspot.in/2012/03/android-apps-break-50mb-barrier.html

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

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 已提供这些库以用于较早的 API 级别。例如,在我的安装中,我在这里找到了 API-8 版本:

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

同一个文件夹在其 zip_file 子文件夹中包含上述 APK 扩展 Zip 库。

I have not personally used this facility yet myself, and so am describing this based solely on the cited references.

我自己还没有亲自使用过这个工具,所以我只是根据引用的参考资料来描述这个。