Data Storage
Data Storage
Preferences
and settings
Lesson 9
● Save new files that the user acquires through your app to
a public directory where other apps can access them and
the user can easily copy them from the device
● Save external files in public directories
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
This work is licensed under a Creative
Android Developer Fundamentals V2 Data storage Commons Attribution 4.0 International 20
License.
Availability of External Storage
• Because the external storage may be unavailable—such as when the user has
mounted the storage to a PC or has removed the SD card that provides the
external storage—you should always verify that the volume is available before
accessing it.
• You can query the external storage state by calling getExternalStorageState().
• If the returned state is equal to MEDIA_MOUNTED, then you can read and
write your files. For example, the following methods are useful to determine
the storage availability:
developer.android.com/reference/android/os/Environment.html
This work is licensed under a Creative
Android Developer Fundamentals V2 Data storage Commons Attribution 4.0 International 24
License.
Accessing public external directories
1. Get a path getExternalStoragePublicDirectory()
2. Create file
● External storage
myFile.delete();
● Internal storage: If the file is saved on internal storage, you can also ask the Context to
locate and delete a file by calling deleteFile():
myContext.deleteFile(fileName);
● You can use the network (when it's available) to store and
retrieve data on your own web-based services
• The automatic backup feature preserves the data your app creates on a user device by uploading it
to the user's Google Drive account and encrypting it.
• There is no charge to you or the user for data storage, and the saved data does not count towards
the user's personal Google Drive quota.
• Once its backed-up data reaches 25MB, the app no longer sends data to the cloud.
• If the system performs a data restore, it uses the last data snapshot that the app had sent to the
cloud.
● Saving Files
● getExternalFilesDir() documentation and code samples
● getExternalStoragePublicDirectory() documentation and code samples
● java.io.File class
● Oracle's Java I/O Tutorial