Java 在Android上保存图片的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/847664/
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-11 20:07:51  来源:igfitidea点击:

Where to save pictures on Android?

javaandroidmediaimage

提问by Ulrich Scheller

My application uses quite a lot of pictures that are downloaded from the internet and cached locally on the Android phone. I am wondering, what is the correct way to save those pictures. There are several ways I see, that are not fully satisfying.

我的应用程序使用了大量从互联网下载并缓存在 Android 手机本地的图片。我想知道,保存这些图片的正确方法是什么。我看到有几种方法并不完全令人满意。

Save them on SD Card in a public folder

将它们保存在 SD 卡上的公共文件夹中

  • Uses up space that wont be freed on uninstall
  • User can see pics in Gallery
  • Needs a folder on the sdcard root (you can actually see it while browsing your phone)
  • 使用了卸载时不会释放的空间
  • 用户可以在图库中查看图片
  • 需要sdcard根目录下的文件夹(你实际上可以在浏览手机时看到它)

Save them on SD Card in a non-public folder

将它们保存在 SD 卡上的非公共文件夹中

  • Uses up space that wont be freed on uninstall
  • Secretly uses space on the SD Card
  • 使用了卸载时不会释放的空间
  • 秘密使用 SD 卡上的空间

Save them inside the application

将它们保存在应用程序中

  • Blows up application size far too much
  • 过分夸大应用程序大小

What is the correct way of locally saving the images of my application to not distract the user and leave no garbage anywhere?

在本地保存我的应用程序图像以不分散用户注意力并且不会在任何地方留下垃圾的正确方法是什么?

采纳答案by Rupert Bates

Your best solution is to use:

您最好的解决方案是使用:

context.getCacheDir()

This directory is private to the application and will be deleted on uninstall, furthermore the system can delete from this directory for you if the device is running short of space.

该目录是应用程序私有的,将在卸载时删除,此外,如果设备空间不足,系统可以为您从该目录中删除。

Note though that the docs say:

请注意,文档说:

you should not rely on the system deleting these files for you; you should always have a reasonable maximum, such as 1 MB, for the amount of space you consume with cache files, and prune those files when exceeding that space

您不应依赖系统为您删除这些文件;您应该始终有一个合理的最大值,例如 1 MB,用于缓存文件消耗的空间量,并在超过该空间时修剪这些文件

If you need a lot of space and would rather use the SD card you can call

如果您需要大量空间并且宁愿使用 SD 卡,您可以致电

getExternalCacheDir()

instead. These will also get removed on uninstall, but the system does not monitor the space available in external storage, so won't automatically delete these files if low on space. If using this option you should also check that external storage is available with

反而。这些也会在卸载时被删除,但系统不会监控外部存储中的可用空间,因此如果空间不足,不会自动删除这些文件。如果使用此选项,您还应该检查外部存储是否可用

Environment.getExternalStorageState()

before attempting to write to it.

在尝试写入之前。

回答by ?mer

I think the best way is to use the database.

我认为最好的方法是使用数据库。

  1. It does not blow up the application and memory.
  2. The related database is deleted once the application is uninstalled.
  3. Nobody can reach to this files besides your application.
  1. 它不会炸毁应用程序和内存。
  2. 卸载应用程序后,相关数据库将被删除。
  3. 除了您的应用程序之外,没有人可以访问此文件。

Update: But; If you want to cache only the data, there is a cache manager defined in webkit. CacheManager

更新:但是;如果只想缓存数据,webkit 中定义了一个缓存管理器。缓存管理器

I didn't use the package before but the methods seem straight forward to use:

我之前没有使用过这个包,但这些方法似乎很容易使用:

static boolean   cacheDisabled()
static boolean   endCacheTransaction()
static CacheManager.CacheResult  getCacheFile(String url, Map<String, String> headers)
static File  getCacheFileBaseDir()
static void  saveCacheFile(String url, CacheManager.CacheResult cacheRet)
static boolean   startCacheTransaction()

and you can find the usage at Google Gears code

您可以在Google Gears 代码中找到用法

I hope this helps.

我希望这有帮助。

回答by Will

If you you don't want to use the CacheManager then use a database or a local (non-SD) file (local files get deleted on a complete uninstall) and register to receive the 'ACTION_DEVICE_STORAGE_LOW' and 'ACTION_DEVICE_STORAGE_OK' broadcast actions. Then you'll know when your application is taking up too much space according to the device and when you need to start deleting pictures. Your application size will still grow, but you will be able to manage the growth and shrinkage.

如果您不想使用 CacheManager,则使用数据库或本地(非 SD)文件(本地文件在完全卸载时被删除)并注册以接收“ACTION_DEVICE_STORAGE_LOW”和“ACTION_DEVICE_STORAGE_OK”广播操作。然后您就会知道您的应用程序何时根据设备占用过多空间以及何时需要开始删除图片。您的应用程序大小仍将增长,但您将能够管理增长和收缩。

回答by Isaac Waller

Just a tip - if you save them on the SD Card, they will be scanned by the MediaScanner and will appear in the users's Gallery (Photos), which you probably don't want. So you probably want to store them on the phone, or somehow tell MediaScanner not to scan them (apparently impossible after a quick Google search.)
Probably best to store a select few in your application's private directory, which will be removed when your application is uninstalled.

只是一个提示 - 如果您将它们保存在 SD 卡上,它们将被 MediaScanner 扫描并显示在您可能不想要的用户图库(照片)中。因此,您可能希望将它们存储在手机上,或者以某种方式告诉 MediaScanner 不要扫描它们(在快速谷歌搜索后显然不可能。)
可能最好在您的应用程序的私有目录中存储一些选择,当您的应用程序被删除时,这些目录将被删除已卸载。

回答by rndstr

You can hide images from the MediaScanner if you put it in a hidden dir(i.e., with a dot prefixed) such as /sdcard/.donotscan/.

如果将图像放在隐藏目录中(即带有点前缀),例如/sdcard/.donotscan/.

Update: As romainguy mentions on twitterthis also works if you put a file named .nomediainto the dir.

更新:正如romainguy 在 twitter 上提到的,如果您将一个名为的文件.nomedia放入目录中,这也有效。