Caution: If you create a shared preferences file with MODE_WORLD_READABLE
or MODE_WORLD_WRITEABLE
, then any other apps that know the file identifier can access your data.
Saving Files
A File
object is suited to reading or writing large amounts of data in start-to-finish order without skipping around. For example, it's good for image files or anything exchanged over a network.
Tip: Although apps are installed onto the internal storage by default, you can specify theandroid:installLocation
attribute in your manifest so your app may be installed on external storage. Users appreciate this option when the APK size is very large and they have an external storage space that's larger than the internal storage. For more information, see App Install Location.
Caution: Currently, all apps have the ability to read the external storage without a special permission. However, this will change in a future release. If your app needs to read the external storage (but not write to it), then you will need to declare the READ_EXTERNAL_STORAGE
permission. To ensure that your app continues to work as expected, you should declare this permission now, before the change takes effect.
<manifest ...> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ... </manifest>
However, if your app uses the WRITE_EXTERNAL_STORAGE
permission, then it implicitly has permission to read the external storage as well.
You don’t need any permissions to save files on the internal storage. Your application always has permission to read and write files in its internal storage directory.
if you need to cache some files, you should instead use createTempFile()
. For example, the following method extracts the file name from a URL
and creates a file with that name in your app's internal cache directory:
public File getTempFile(Context context, String url) { File file; try { String fileName = Uri.parse(url).getLastPathSegment(); file = File.createTempFile(fileName, null, context.getCacheDir()); catch (IOException e) { // Error while creating file } return file; }
Note: Your app's internal storage directory is specified by your app's package name in a special location of the Android file system. Technically, another app can read your internal files if you set the file mode to be readable. However, the other app would also need to know your app package name and file names. Other apps cannot browse your internal directories and do not have read or write access unless you explicitly set the files to be readable or writable. So as long as you use MODE_PRIVATE
for your files on the internal storage, they are never accessible to other apps.
Query Free Space
If you know ahead of time how much data you're saving, you can find out whether sufficient space is available without causing an IOException
by calling getFreeSpace()
or getTotalSpace()
. These methods provide the current available space and the total space in the storage volume, respectively. This information is also useful to avoid filling the storage volume above a certain threshold.
However, the system does not guarantee that you can write as many bytes as are indicated by getFreeSpace()
. If the number returned is a few MB more than the size of the data you want to save, or if the file system is less than 90% full, then it's probably safe to proceed. Otherwise, you probably shouldn't write to storage.
Note: You aren't required to check the amount of available space before you save your file. You can instead try writing the file right away, then catch an IOException
if one occurs. You may need to do this if you don't know exactly how much space you need. For example, if you change the file's encoding before you save it by converting a PNG image to JPEG, you won't know the file's size beforehand.
Delect a File
You should always delete files that you no longer need. The most straightforward way to delete a file is to have the opened file reference call delete()
on itself.
myFile.delete();
If the file is saved on internal storage, you can also ask the Context
to locate and delete a file by callingdeleteFile()
:
myContext.deleteFile(fileName);
Note: When the user uninstalls your app, the Android system deletes the following:
- All files you saved on internal storage
- All files you saved on external storage using
getExternalFilesDir()
.
However, you should manually delete all cached files created with getCacheDir()
on a regular basis and also regularly delete other files you no longer need.
Saving Data in SQL Databases