简体   繁体   English

在Android上保存图片的位置?

[英]Where to save pictures on Android?

My application uses quite a lot of pictures that are downloaded from the internet and cached locally on the Android phone. 我的应用程序使用了很多从互联网上下载并在Android手机上本地缓存的图片。 I am wondering, what is the correct way to save those pictures. 我想知道,保存这些图片的正确方法是什么。 There are several ways I see, that are not fully satisfying. 我看到有几种方式,但并不完全令人满意。

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 用户可以在Gallery中看到图片
  • Needs a folder on the sdcard root (you can actually see it while browsing your phone) 在SD卡根目录上需要一个文件夹(您可以在浏览手机时看到它)

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? 本地保存应用程序图像的正确方法是什么,以免分散用户的注意力并且不留垃圾?

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. 在尝试写入之前。

You can hide images from the MediaScanner if you put it in a hidden dir (ie, with a dot prefixed) such as /sdcard/.donotscan/ . 如果将图像放在隐藏目录 (即带有前缀的点)(例如/sdcard/.donotscan/ )中,则可以隐藏MediaScanner中的图像。

Update: As romainguy mentions on twitter this also works if you put a file named .nomedia into the dir. 更新:正如romainguy在twitter上提到的那样,如果你把一个名为.nomedia的文件放到dir 中,这也有效。

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. 除了您的应用程序,没有人可以访问此文件。

Update: But; 更新:但是; If you want to cache only the data, there is a cache manager defined in webkit. 如果要仅缓存数据,则在webkit中定义缓存管理器。 CacheManager 的CacheManager

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. 我希望这有帮助。

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. 如果您不想使用CacheManager,则使用数据库或本地(非SD)文件(本地文件在完全卸载时被删除)并注册以接收'ACTION_DEVICE_STORAGE_LOW'和'ACTION_DEVICE_STORAGE_OK'广播操作。 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. 您的应用程序规模仍将增长,但您将能够管理增长和缩减。

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. 只是提示 - 如果您将它们保存在SD卡上,它们将被MediaScanner扫描并显示在您可能不想要的用户的图库(照片)中。 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.) 因此,您可能希望将它们存储在手机上,或以某种方式告诉MediaScanner不要扫描它们(在快速谷歌搜索后显然是不可能的。)
Probably best to store a select few in your application's private directory, which will be removed when your application is uninstalled. 可能最好将少数几个存储在应用程序的私有目录中,这将在卸载应用程序时删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM