Best Practice when Caching files in Android

前端 未结 3 1057
执念已碎
执念已碎 2021-01-30 02:42

I currently have my app caching image files in the cache sub-directory for the application. The images are used in a ListView and stored in a Has

3条回答
  •  失恋的感觉
    2021-01-30 03:22

    I can't offer you a comprehensive set of best practices, but I can offer what I've learned so far:

    Managing your cache is a good idea. My app's cache is such that I know that I'll never need more than a certain number of cached files, so whenever I insert a new file into the cache, I delete the oldest files until I'm under the limit I have set. You could do something similar based on size, or simply age.

    Caching to the SD card, if it's available, is a good idea if your cache needs to take up a lot of space. You'll need to manage that space just as carefully, since it won't automatically clear that space for you. If you're caching image files, be sure to put them in a directory that begins with a dot, like "/yourAppHere/.cache". This will keep the images from showing up in the gallery, which is really annoying.

    Letting the user choose the location of the cache seems like overkill to me, but if your audience is very geeky, it might be appreciated.

    I haven't noticed a much of a penalty when caching to the SD, but I don't know how your app uses that information.

提交回复
热议问题