Where to save pictures on Android?

33

23

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 those pictures. There are several ways I see, that are not fully satisfying.

Save them on SD Card in a public folder

  • Uses up space that wont be freed on uninstall
  • User can see pics in Gallery
  • Needs a folder on the sdcard root (you can actually see it while browsing your phone)

Save them on SD Card in a non-public folder

  • Uses up space that wont be freed on uninstall
  • Secretly uses space on the SD Card

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?

Ulrich Scheller

Posted 2009-05-11T11:18:17.220

Reputation: 10 354

Answers

30

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.

Rupert Bates

Posted 2009-05-11T11:18:17.220

Reputation: 2 716

great answer looking for such an idea since very long time. – Akram – 2012-03-03T06:37:03.483

11

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.

rndstr

Posted 2009-05-11T11:18:17.220

Reputation: 729

6

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. 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.

Ömer

Posted 2009-05-11T11:18:17.220

Reputation: 1 318

1I am using the database for storing some other data and it does add up to the application size. You can see it in Settings/Applications/Manage Applications. – Ulrich Scheller – 2009-05-12T08:38:45.143

1The best method for you is caching the files using CacheManager. I updated my answer. Please look at my update. – Ömer – 2009-05-12T11:11:40.557

3I disagree that CacheManager is the best solution, its use is deprecated. To quote from the docs: "This class is deprecated. Access to the HTTP cache will be removed in a future release." – Rupert Bates – 2011-05-09T12:35:25.890

0

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. 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.

Will

Posted 2009-05-11T11:18:17.220

Reputation: 9 193

-1

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. 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.)
Probably best to store a select few in your application's private directory, which will be removed when your application is uninstalled.

Isaac Waller

Posted 2009-05-11T11:18:17.220

Reputation: 16 706