Where to save pictures on Android?
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.
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/
.
Update: As romainguy mentions on twitter this also works if you put a file named .nomedia
into the dir.
I think the best way is to use the database.
- It does not blow up the application and memory.
- The related database is deleted once the application is uninstalled.
- 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. 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
I hope this helps.