Where to save pictures on Android?

前端 未结 5 2057
被撕碎了的回忆
被撕碎了的回忆 2020-12-22 23:06

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 th

5条回答
  •  萌比男神i
    2020-12-22 23:38

    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

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

    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.

提交回复
热议问题