When would you save data to the internal cache directory? - android

I am reading through the Android developer docs on saving data and they mention that when saving a file to internal storage you can use either getFilesDir() or getCacheDir(). When would it be appropriate to save to the cache directory, and what type of data is best saved here?

The getCacheDir() should only be used for data that is not essential for the running of the app. In other words, you can store data which if unavailable, can be fetched from the server. The reason for this is that the system may delete all the files in the cache dir to reclaim storage space.
http://developer.android.com/training/basics/data-storage/files.html

Use the cache directory to save temporary files. For example, if you're writing a social app (like instagram) and during runtime the app loads user's pictures, you can store those pictures on the cache directory, so if the user resumes the app while offline, he will still be able to see the last pics.
Make sure to delete each file once it is no longer needed and implement a reasonable size limit for the amount of memory you use at any given time, such as 1MB.

Related

where to persist multiple image files on android

I'm writing an android app that makes use of many images(dozens). These images, like most of the other data in the app, are updated from a remote database. For the data I am going to have a local database and sync it with the remote database every time the remote db is updated.
The remote database will store the images as URLs, and my app will download the images from these urls to display them in the app. So I could just have my local database sync with the remote one and I'll have the URL of all the image files I'm using and I can re download them from the remote server every time the app is run, but this is obviously slow and wastes a lot of data.
What I want to do is, everytime the database is updated and needs to be synced, the app will sync its local db and download the new images from their URLs as usual, but then it will save the image files somewhere on the device, so next time the app is run it can just grab the images from the device.
I can't seem to find an effective way of doing this, perhaps it's because it's a bad idea to do it this way in the first place? Sharepreferences probably won't have enough room, external storage isn't available all the time, and I hear it's a bad idea to just store a big chunk of binary data(such as an image) in the local database.
What are my options here?
Start with Android Storage Options.
Further:
external storage isn't available all the time
That's true in theory, but in practice, you'll have external storage 99% of the time, especially if you're developing for newer devices. Note that "external storage" and SD Card aren't the same thing -- the terminology here is confusing. For this, use getExternalFilesDir().
Regardless, if your image storage is a reasonable amount (<100MB is reasonable IMHO), then you can just use internal storage. As of Android 3.x, this won't cause a device to run out of space like early devices did. In practice, you'll be fine 99% of the time. Here you might want to use the cache dir, returned by getCacheDir().
In one of my apps I have a large number of files that are synced with a CDN. I use the sync process and database to retrieve download URLs and MD5 hashes of the files. If the MD5 hash has changed, then I download the file again in the background. I simply store the file using the hash as the file-name, so I can easily resolve duplicate files. Periodically, I also walk the cacheDir to see whether there are any files that are no longer referenced in the database.

Saving Data Privately On Device

I have files that I want to save so they are not accessible by other applications and that are safe from updates (won't be deleted).
My Problem
I am not sure where to save them. I know that I can save them in the data directory using the below code to get the path but I'm not sure if this is correct.
Environment.getDataDirectory();
My Question
Is the applications data directory the correct place to store my data or is it meant just for system data?
If it isn't the correct place, could you suggest where is?
Thanks in advance
I'm sure you have already come across this
http://developer.android.com/guide/topics/data/data-storage.html
In that you see Internal storage is usually a methodology which helps in achieving what you asked for. Yes you can use the function which you have shown
or
(OWNERACTIVITY).getFilesDir().getAbsolutePath()
should give you the location where data for the application gets stored.
If you are keeping files in SD card, all applications (with android.permission.READ_EXTERNAL_STORAGE) can access the data. The best place is is keep them in your data directory. Your application can access the data as long as your package name and certificate is not changed. If you want to store some files like images and audio, you can store them in SD card in a directory with .nomedia in it. But if you want to keep some data files, checksums etc, it is better to keep them in data directory. Even if you update your application, you should still be able to access it.
YOu can read more here

How to move the database from internal memory to external memory?

I'm storing few sms messages and their details in a database. By default the database is stored in internal memory. I would like to check for the internal memory before accessing the database and once it is full I would like to move the database to external memory. How to handle this?
Pretty much not sure about what you exactly doing up there. Whereas what I do is, there is an app in android market name sms backup and restore. Just run that utility which will save your messages in a xml file in the external storage sd-card. You can move the file wherever you want and xml can be read and write easily too. You can even restore the xml file with the same application to any other android device or into your own device.

Android - Design Pattern for Storing Indeterminate Amounts and Sizes of Images

I am trying to decide where to store images that are sent as part of instant messages coming in to an app. These messages are viewable in a conversation history view for sometimes a significant period of time after their original receipt. You can imagine any number of other use cases that would have a similar requirement, so the question here is on the "best practice for storing an indeterminate quantity and size of images"
Assumptions
SQLite storage is clearly a bad option since the image size is not
bounded.
It is neither desirable nor undesirable that these images be publicly available to other apps or discoverable by MediaScanner. We are assumed to be perfectly neutral on this point...
This leaves two parts to this question:
1. External Storage
It seems like external storage is to be preferred when available because it is likely to have more room than anything else:
The documentation says the following:
...use getExternalCacheDir() to
open a File that represents the external storage directory where you
should save cache files. If the user uninstalls your application,
these files will be automatically deleted. However, during the life of
your application, you should manage these cache files and remove those
that aren't needed in order to preserve file space.
Unlike internal storage cache, there is no statement made about the automatic reclamation of space on external storage by Android. Still the word "cache" makes me nervous.
Question 1: Do these files remain until explicitly deleted regardless?
Question 2: Is there any other external storage other than the cache that is automatically deleted upon app uninstall AND is preferable to the external cache for some specific reason?
2. Internal Storage
Clearly not every device has external storage, so there needs to be a provision for internal storage.
Question 3: Is the only practical difference between the internal cache retrieved through getCacheDir() and files created with openFileOutput(FILENAME, Context.MODE_PRIVATE) that Android may delete files in the cache directory when under pressure for storage space?
Do these files remain until explicitly deleted regardless?
I haven't read the code, but the javadoc explicitely says
The platform does not monitor the space available in external storage, and thus will not automatically delete these files. Note that you should be managing the maximum space you will use for these anyway, just like with getCacheDir().
Is there any other external storage other than the cache that is automatically deleted upon app uninstall AND is preferable?
None that I know of.
practical difference between the internal cache retrieved through getCacheDir() and files created with openFileOutput?
It's just a facility method, AFAIK

How do you make cache folder content deleteable?

Okay so I've noticed that even though I use the correct path for the cache folder Android doesn't register the content in the folder so the user can't delete the cache content by going into settings -> programs -> administrate -> select program -> clear cache. The folder is deleted properly on uninstal but not if the user actively try to clear the cache. This is not a major issue but its still a minor problem because the user don't get a proper idea of how much space the application uses at the SD card.
Is there anything I as developer can do to update these values or am I doing something wrong somewhere else?
From the docs:
Saving cache files
If you'd like to cache some data, rather than store it persistently,
you should use getCacheDir() to open a File that represents the
internal directory where your application should save temporary cache
files.
When the device is low on internal storage space, Android may delete
these cache files to recover space. However, you should not rely on
the system to clean up these files for you. You should always maintain
the cache files yourself and stay within a reasonable limit of space
consumed, such as 1MB. When the user uninstalls your application,
these files are removed.
I guess you should handle the removal of yourself. If you want to remove the content if the user cleans the app data create a sharedPreference and clean the cache when the app starts and that preference is not defined.

Categories

Resources