views:

5926

answers:

4

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?

+3  A: 

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.

Omer
I 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
The best method for you is caching the files using CacheManager. I updated my answer. Please look at my update.
Omer
+1  A: 

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
+1  A: 

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
+4  A: 

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

rndstr