Students will understand how to store data using shared preferences and internal and external storage.
Create a
User
class. A User has a name and an age. Create a constructor and getters and setters for this object, and override toString so that it returns "$name, $age".
Create an app with one main activity. The activity should include an EditText that takes in a user's name and a number representing the user's age, and a submission button.
When a user hits the submit button, use the name and age fields to create a
User
object with that name and age. Store theUsers
in an array.
Shared Preferences are a way to store persistent key-value pairs. You can have multiple Shared Preferences files, or just one, and store and retrieve data with methods such as putBoolean
and getString
. Example code can be found here.
Exercise
Add a
total_users
counter to your app. Every time a user gets added, increment this value and toast how many users you've seen. Store/retrievetotal_users
in shared preferences so that this number always increases across uses.
File - The File object represents a File and can be used to create, delete, read from, and write to a File. You can also use this object to manage File permissions and metadata.
An Object is serialized when it is turned into a stream of bytes so it can be written to a file, and deserialized when it is turned back into an Object using the stream of bytes. An Object that does these can implement the Serializable interface. In order to be serializable and deserializable, you can create/use ObjectOutputStream and ObjectInputStream.
Exercise
Make the
User
objectSerializable
.
Add a spinner (dropdown), and populate the spinner with your
Users
array.
One of the tricky things about writing code for mobile devices is the limited amount of free space (and resources in general, including local resources all well as non-gaurantees of internet connectivity and bandwidth). When you run out of space, you will get an IOException. If you know (or can ballpark) the amount of space you'll need beforehand, you can use getFreeSpace()
and getTotalSpace()
on the File
object to know if you'll have enough space.
All devices have internal storage (built-in storage). External storage is typically a device such as an SD card, although sometimes the external storage is nonremovable. Internal storage is always available (unlike external storage). When a user deletes your app, the associated internal storage is deleted by default, but the associated external storage is not deleted by default. Interal storage is typically only meant to be read by the app, wheras external storage is world-readable. Internal storage is more suited to something like a database; external storage might be for memes that a user has created.
To get the files directory, use getFilesDir()
which will tell you the path of where the files are stored.
Create a FileOutputStream
object, write to the stream, and then close it:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
See also Using Internal Storage and Save a File on Internal Storage.
Exercise
Using the serialized
User
object, whenever a newUser
is added, add it as a line in the file. Use this file to populate the spinner.
In the Manifest file, you must add permissions to access external storage. In order to read from external storage, include the line:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
In order to write to external storage, include the line:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Note that, as with the File object, read permissions and write permissions are separate (as are execution permissions). This division of permissions is typical.
Check Environment.getExternalStorageState()
. The state may read and write, read only, or not avaialble.
Get the public files directory using getExternalStoragePublicDirectory()
or getExternalFilesDir()
for private files.
Write to the file stream and close.
See also Using External Storage and Save a File on External Storage.
Exercise
Do the same thing, saving the serialized
User
object and populating the spinner, but instead saving to and reading from external storage.
Delete a File
with the delete()
method, or by calling deleteFile()
on the Context
.
Exercise
Add a "clear all" button to the main activity. When this button gets pressed, delete the file.
(Accessing a file with ADB etc.)