Data Storage tutorial, basic samples are included
- Data Storage Methods
- Preferences Preferences is a lightweight mechanism to store and retrieve key-value pairs of primitive data types.
- Files You can store your data in files on your mobile phone, or in a removable storage medium.
- Databases Android Api supports SQLite databases. All databases, SQLite and others, are stored on the device in /data/data/package_name/databa
- Network You can also use the Internet to store and receive data, whether it's an SQLite database, or just a simple textfile.
Examples
Preferences
- // Restore preferences
- SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
"PREFS_NAME" is a String, for instance "myData"(.xml). You should know that it stores key- value pairs in .xml format.
- // Save preferences SharedPreferences settings = getSharedPreferences(PREFS_NAM
E, 0); - SharedPreferences.Editor editor = settings.edit();
- // Don't forget to commit your edits!!!
- editor.commit();
That is it.
Files
Writing to the SD Card:
- // Reading it back..
Writing to files with Serialization / writing Objects to file:
- oos.writeObject(this); oos.close();
- // Reading it back..
- Flow f = (Flow) ois.readObject();
Network
You can also use Internet to store your data. Here's an example code how can you upload your file(s) to you remote web server:
- HttpRequest request = new HttpRequest("http://host/some_path");
- Part[] parts = { new StringPart("param_name", "value"), new FilePart(f.getName(), f) };
- filePost.setEntity( new MultipartRequestEntity(parts, filePost.getParams()) );
- HttpClient client = new HttpClient();
- int status = client.executeMethod(filePost);
Database
Storing your data in databases are not that simple like store key-value pairs in Preferences. That's why I'm going to show you the Database method in a next tutorial.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@619Apps (iPhone App Developer)'Xperia Sola' trademark hints at another possible Sony handset for the U.S. - http://t.co/atxhdfFz #iPhone #android #apps
2 hours 34 min ago -
@ayakaarchdia233 (Love)I've just received an achievement: Discriminating Shopper https://t.co/LDVOSV6I #Android #Androidgames
2 hours 34 min ago -
@DevrynBluelagon (Devryn Bluelagon)I've just received an achievement: Novice Photographer https://t.co/NpjOoveN #Android #Androidgames
2 hours 34 min ago -
@games_lma (leila marie ashley)I've just received an achievement: Persistent Shopper https://t.co/vTs6DSor #Android #Androidgames
2 hours 34 min ago -
@hawkhugh (hawkhugh)Apple's iPad3 http://t.co/T9dSUJA9 #apple #ipad3 #iphone #android
2 hours 34 min ago
Poll
Useful resources
Android Development Projects
- Android App: GPS: Form: Database: Website by danishayubb
- Jquery Mobile Project 01 by menfirst
- Simple Android App to load our Mobile Site - Hiring NOW! by steadysystems
- mobile app by ultimaterrrr
- android apps by vaneet08
- Online food shopping app (anroid) by akshaynawale
- Android eBook / reader Application by bamohriz
- Android app 50k downloads by nhcteam
- Beautiful Android Live Wallpaper by wahid2o11
- App downloads by nhcteam



