Chapter 5 - Android Data Management
Chapter 5 - Android Data Management
Data management
Chapter 5
Data: outline
Direct File I/O: Read/write files onboard or on SD cards. Remember to req uest
permission for writing, for instance, on SD card
Application Direct Access: Read only access from res assets/raw directories
Increase functionality:
Content Providers: expose data to other applications
Services: background processes that run detached from any view
Preference system
Shared preferences
getSharedPreferences(String name, Context.MODE_WORLD_READABLE);
getSharedPreferences(String name, Context.MODE_WORLD_WRITABLE);
Linux architecture
User privileges
– Quite limited
Onboard data
– Application's reserved data
External data
– SD card (/mnt/sdcard)
File I/O
Onboard
– Write to a designated place for each application
– Where? /data/data/<package>/files
– How? Use standard java I/O classes
SD card
– Where? Environment.getExternalStorageDirectory()
– How? Use standard java I/O classes
– Permissions? android.permission.WRITE_EXTERNAL_STORAGE
Raw Text Files: how?
XML File
Place it under res/xml/ directory
Start the file with
<?xml version=“1.0” encoding=“utf-8”?>
Add whatever you want with <mytag>value</mytag>
XML Files: example
Define the DB
Create a class that extends
android.content.ContentProvider
Implement query(), insert(), update(), delete()
Register the ContentProvider in the manifest
How to use a Content Provider
Content
Provider
Example: contacts
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Saving Data in Files in android
1. Internal Storage
You can save files to the internal storage of the
device, which is private to your application.
2. External Storage
You can also save files to external storage, which
can be accessed by other applications.
Saving Data, Cont.….
3. Shared Preferences
For saving small amounts of data, such as user preferences,
you can use Shared Preferences.
4. Using SQLite Database
For structured data that requires complex queries, you might
consider using an SQLite database.
Conclusion
Choose the method that best fits your needs based on the type and
amount of data you want to store. For simple key-value pairs, Shared
Preferences are sufficient; for larger files, consider internal or external
storage; and for structured data, an SQLite database is appropriate.