Document directory
- Use sharedpreferences to store data
- File Storage Data
- Network Storage Data
- Summary
The first two articles: Android SQLite parsing and data sharing between Android applications explain in detail how to use the database to store information and how to use contentprovider to obtain data shared by other applications, now we will summarize the android data storage, and select an appropriate data storage method as needed in the future development process.
Android provides five data storage methods:
- Use sharedpreferences to store data;
- File storage data;
- SQLite database stores data;
- Use contentprovider to store data;
- Network Storage data;
3 and 4 have been described in detail in the data sharing space between Android SQLite parsing and Android applications. I will not repeat the description here. I will introduce the other three methods in detail.
Use sharedpreferences to store data
First, describe the sharedpreferences storage method. It is a mechanism provided by Android to store some simple configuration information, such as the username and password of the login user. It uses the map data structure to store data, and stores data in key-value mode, which can be simply read and written. The specific example is as follows:
Void readsharedpreferences ()
{
String strname, strpassword;
Sharedpreferences user = getsharedpreferences ("user_info", 0 );
Strname = user. getstring ("name ","");
Strpassword = user getstring ("password ","");
}
Void writesharedpreferences (string strname, string strpassword)
{
Sharedpreferences user = getsharedpreferences ("user_info", 0 );
Uer. Edit ();
User. putstring ("name", strname );
User. putstring ("password", strpassword );
User. Commit ();
}
The Data Reading and writing methods are very simple, but there are some differences when writing: First call edit () to make it edit, then you can modify the data, and finally use commit () submit the modified data. In fact, sharedpreferences stores data to the device in XML format, under/data/<package name>/shares_prefs in file explorer of ddms. The preceding data storage result is used as an example. After opening the file, you can see the user_info.xml file. After opening the file, you can see:
<? XML version = "1.0" encoding = "UTF-8"?>
<Map>
<String name = "name"> moandroid </string>
<String name = "password"> sharedpreferences </string>
</Map>
Sharedpreferences is restricted: it can only be used in the same package and cannot be used between different packages.
File Storage Data
The file storage method is a commonly used method. The method for reading/writing files in Android is exactly the same as that in Java for implementing I/O. It provides openfileinput () and openfileoutput () to read files on the device. Filterinputstream,
Filteroutputstream can be detailed in the Java Io package description:
String fn = "moandroid. log ";
Fileinputstream FCM = openfileinput (FN );
Fileoutputstream Fos = openfileoutput (FN, context. mode_private );
In addition, Android provides other functions to operate files. For more information, see Android SDK.
Network Storage Data
Network Storage Methods need to deal with Android Network data packets. For more information about Android Network data packets, see android
Which packages does the SDK reference for Java SDK ?.
Summary
In the above five storage methods, we will find a suitable data storage method based on the design objectives, performance requirements, and space requirements in the future development process. Data storage in Android is private and cannot be accessed by other applications unless the data shared by other applications is obtained through contentresolver.
Copy search
Copy search