Harsha M V
Harsha M V

Reputation: 54949

Android File Caching

Which is the best place to cache images and other files on the SD card ?

I have been doing it under SD/{application_name}/cache

is this the right place to cache the files ?

Upvotes: 0

Views: 7407

Answers (2)

Padma Kumar
Padma Kumar

Reputation: 20041

Accessing files on external storage

If the user uninstalls your application, this directory and all its contents will be deleted.

on API level 8 or greater use the external cache directory: http://developer.android.com/guide/topics/data/data-storage.html#ExternalCache

There is also an explanation for using API level 7 and lower in the above link

Saving files that should be shared

If you want to save files that are not specific to your application and that should not be deleted when your application is uninstalled, save them to one of the public directories on the external storage. These directories lay at the root of the external storage, such as Music/, Pictures/, Ringtones/, and others.

In API Level 8 or greater, use getExternalStoragePublicDirectory(), passing it the type of public directory you want, such as DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES, or others. This method will create the appropriate directory if necessary.

If you're using API Level 7 or lower, use getExternalStorageDirectory() to open a File that represents the root of the external storage, then save your shared files in one of the following directories:

Music/ - Media scanner classifies all media found here as user music.
Podcasts/ - Media scanner classifies all media found here as a podcast.
Ringtones/ - Media scanner classifies all media found here as a ringtone.
Alarms/ - Media scanner classifies all media found here as an alarm sound.
Notifications/ - Media scanner classifies all media found here as a notification sound.
Pictures/ - All photos (excluding those taken with the camera).
Movies/ - All movies (excluding those taken with the camcorder).
Download/ - Miscellaneous downloads.

Upvotes: 1

Michele
Michele

Reputation: 6131

The getCacheDir Method points to the same file hierarchy you proposed but on the build in memory. You are save when using this method to determine the cachedir for your pictures. Keep in mind that you have to manage a cachedirectory on the SD Card on your own. The getCacheDir Directory is managed by the OS.

Context.getCacheDir

Upvotes: 1

Related Questions