Home » Android » java – Where to save pictures on Android?

java – Where to save pictures on Android?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:

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.

Answer:

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.

Answer:

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.

Answer:

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.

Answer:

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.