Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to save pictures on Android?

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?

like image 314
Ulrich Scheller Avatar asked May 11 '09 11:05

Ulrich Scheller


People also ask

How do I save photos on an Android?

Touch and hold the image. Select a save option (e.g., Save attachment, Save to SD card, etc.). Unless otherwise specified, the image is saved to the default picture/video location (e.g., Gallery, Photos, etc.).

Where is the gallery stored in Android?

The photos you taken on phone camera will be saved under dcim folder in the internal storage or filemanager in android mobiles,so if you want to open gallery photos in file manager then click on DCIM folder and then click on camera to view taken photos and videos of your mobile.

Where can I save my pictures on my phone?

One of the best of these options is Google Photos. Google's free app allows anyone to upload their photos and videos to the cloud. Even better, it comes with free unlimited storage for videos (up to 1080p) and photos (up to 16-megapixels).


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

like image 75
Rupert Bates Avatar answered Sep 19 '22 15:09

Rupert Bates


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.

like image 40
rndstr Avatar answered Sep 16 '22 15:09

rndstr