解决方案SD卡上清洁的图像缓存目录 [英] Solution for cleaning an image cache directory on the SD card

查看:223
本文介绍了解决方案SD卡上清洁的图像缓存目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了主要是基于遥感影像的应用程序。它们通常是一起在ListView一些数据显示。很多这些图像是新的,很多旧的将永远不会再看到。

我目前所有存储在自定义缓存目录SD卡上的这些图像(ALA evancharlton的Magnatune的应用程序)。

我注意到,后10天左右,该目录总计30MB〜。这比我预期的相当多,这使我相信,我需要拿出清理旧文件一个好的解决办法......我只是不认为一个伟大的。也许你可以提供帮助。这些是我有过的想法:


  1. 删除旧文件。当应用程序启动时,启动一个后台线程,并删除早于X天的所有文件。这似乎是造成问题,虽然,在,如果用户活跃地使用的应用程序,这样可以使设备迟缓,如果有数百个文件删除的


  2. 在SD卡上的文件建立后,调用
    文件(/路径/到/文件)deleteOnExit();
    这将导致删除所有文件时,VM退出(我甚至不知道这个方法适用于Android)。这是可以接受的,因为,即使文件需要被缓存的会话,它们并不需要被缓存,以用于下一个会话。看起来这也将减缓设备下来,如果有大量的文件被删除时,虚拟机退出。


  3. 删除旧文件,到文件的最大数量。同#1,但只有一次删除文件的N多。我真的不喜欢这个想法,如果用户非常活跃,它可能永远无法赶上并保持高速缓存目录的清洁。


这是关于我的一切。任何建议将是AP preciated。


解决方案

  1. 请不要一次将它们全部删除。删除每隔数秒或东西,用户可能没有注意到。

  2. 虚拟机不会在Android上正常退出,所以 deleteOnExit()将不可靠。

  3. 参见上面#1。

您也可以考虑使用 AlarmManager 安排在早上凌晨删除工作。这有一个封端的CPU的一个附带的好处击中 - 运行真正在后台被盖住给CPU的〜10%任何东西,所以这个工作不会产生影响,即使用户实际使用该设备在那个时刻的用户。您将需要使用激活锁定来保持清醒的设备,而你要删除的东西。一种可能性是用我的 WakefulIntentService 以这一点,因为它解决了如何使设备保持清醒和有它做删除工作过主应用程序线程的问题。

I've got an app that is heavily based on remote images. They are usually displayed alongside some data in a ListView. A lot of these images are new, and a lot of the old ones will never be seen again.

I'm currently storing all of these images on the SD card in a custom cache directory (ala evancharlton's magnatune app).

I noticed that after about 10 days, the directory totals ~30MB. This is quite a bit more than I expected, and it leads me to believe that I need to come up with a good solution for cleaning out old files... and I just can't think of a great one. Maybe you can help. These are the ideas that I've had:

  1. Delete old files. When the app starts, start a background thread, and delete all files older than X days. This seems to pose a problem, though, in that, if the user actively uses the app, this could make the device sluggish if there are hundreds of files to delete.

  2. After creating the files on the SD card, call new File("/path/to/file").deleteOnExit(); This will cause all files to be deleted when the VM exits (I don't even know if this method works on Android). This is acceptable, because, even though the files need to be cached for the session, they don't need to be cached for the next session. It seems like this will also slow the device down if there are a lot of files to be deleted when the VM exits.

  3. Delete old files, up to a max number of files. Same as #1, but only delete N number of files at a time. I don't really like this idea, and if the user was very active, it may never be able to catch up and keep the cache directory clean.

That's about all I've got. Any suggestions would be appreciated.

解决方案

  1. Don't delete them all at once. Delete one every few seconds or something, and the user may not notice.
  2. The VM does not exit normally on Android, so deleteOnExit() will not be reliable.
  3. See #1 above.

You might also consider using AlarmManager to schedule deletion work for the wee hours of the morning. This has a side benefit of a capped CPU hit -- anything that runs truly in the background is capped to ~10% of CPU, so this work will not impact the user even if the user is actually using the device at that hour. You will need to use a WakeLock to keep the device awake while you are deleting things. One possibility is to use my WakefulIntentService for this, as it solves the problem of keeping the device awake and having it do the deletion work off the main application thread.

这篇关于解决方案SD卡上清洁的图像缓存目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆