Five data storage methods in Android
Data storage is most frequently used during development. Here we mainly introduce five Data Storage Methods on the Android platform:
1. Use SharedPreferences to store data;
2. File storage data;
3. The SQLite database stores data;
4. Use ContentProvider to store data;
5. network storage data;
Five data storage methods in Android
Introduction: This is a detailed page of methods in data storage in Android 5. It introduces the knowledge related to the methods in mobile software, data storage in Android 5, and Android data storage.
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;
The three methods, Preference, File, and DataBase, respectively correspond to/data/Package.
Name/Shared_Pref,/data/Package Name/files,/data/Package
Name/database. In Android, the File storage method is usually Context. openFileOutput (String fileName, int mode) and Context. openFileInput (String fileName ).
Context. openFileOutput (String fileName, int
Mode) the generated files are automatically stored in the/data/Package Name/files directory. The full path is/data/Package.
Name/files/fileName. Note that the fileName parameter here cannot contain path delimiters (such "/").
Generally, files generated in this way can only be accessed in this apk. However, the conclusion is that Context. openFileInput (String
FileName. In this way, each apk can only access its/data/Package
The file in the Name/files directory is very simple. The fileName parameter cannot contain path delimiters. Android will automatically
Find the file named fileName in the/Package Name/files directory. I. 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 changes
. In fact, SharedPreferences uses XML format to store data to the device.
/Data/<package in Explorer
Name>/shares_prefs. 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. Ii. File Storage Data
The file storage method is a common method for reading/writing files in Android.
Java provides the openFileInput () and openFileOutput () Methods to read files on the device.
FilterInputStream and FilterOutputStream can be used in Java io
Go to the package description for details:
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.
Iii. Network-based data storage methods, which need to deal with Android Network data packets. For details about Android Network data packets, please refer to the packages referenced by Java SDK in Android SDK ?. Iv. ContentProvider
1. Introduction to ContentProvider
When an application inherits the ContentProvider class and overwrites the class to provide and store data, it can share its data with other applications. Although other methods can be used to share data externally
Data, but the data access method varies depending on the data storage method. For example, to share data with external users through files, you need to perform file operations to read and write data. sharedpreferences is used to share data.
You must use the sharedpreferences API to read and write data. The advantage of using ContentProvider to share data is to unify the data access mode .?
2. Uri class Introduction
Uri indicates the data to be operated. Uri contains two parts: 1. contentProvider, 2. a Uri consists of the following parts:
1. scheme: the content provider's scheme has been defined by Android as: content ://.
2. Host Name (or Authority): Used to uniquely identify the ContentProvider, which can be found by external callers.
3. path: it can be used to represent the data to be operated. The path construction should be based on the business, as shown below:
? To operate records with the id of 10 in the contact table, you can build the following path:/contact/10.
? Name field of the record whose id is 10 in the contact table, contact/10/name
? To operate all records in the contact table, you can build the path:/contact?
The data to be operated may not necessarily come from the database or be stored in files, as follows:
To operate the name node under the contact node in the xml file, you can build the path:/contact/name
To convert a string to a Uri, you can use the parse () method in the Uri class as follows:
Uri uri = Uri. parse ("content: // com. changcheng. provider. contactprovider/contact ")
3. Introduction to UriMatcher, ContentUrist, and ContentResolver
Because Uri represents the data to be operated, we often need to parse the Uri and obtain data from the Uri. The Android system provides two tool classes for Uri operations: UriMatcher and ContentUris. Understanding their usage will facilitate our development work.
? UriMatcher: Used to match a Uri. Its usage is as follows:
1. First, you need to match all the Uri paths to the registration, as shown below:
// Constant UriMatcher. NO_MATCH indicates the return code (-1) that does not match any path ).
UriMatcher uriMatcher = new UriMatcher (UriMatcher. NO_MATCH );
// If the match () method matches the content: // com. changcheng. sqlite. provider. contactprovider/contact path, 1 is returned.
UriMatcher. addURI ("com. changcheng. sqlite. provider. contactprovider", "contact", 1); // Add a uri that needs to be matched. If yes, a matching code is returned.
// If the match () method matches the content: // com. changcheng. sqlite. provider. contactprovider/contact/230 path, the matching code is 2.
UriMatcher. addURI ("com. changcheng. sqlite. provider. contactprovider", "contact/#", 2); // # It is a wildcard
2. After registering the Uri to be matched, you can use the uriMatcher. match (uri) method to match the input Uri. If the Uri matches, a matching code is returned.
The third parameter passed in by the addURI () method.
Content: // com. changcheng. sqlite. provider. contactprovider/contact path, matching returned
The code is 1.
?
ContentUris: used to obtain the ID part after the Uri path. It has two practical methods:
? WithAppendedId (uri, id) is used to add the ID part to the path.
? The parseId (uri) method is used to obtain the ID part from the path.
?
ContentResolver: When an external application needs to add, delete, modify, and query data in ContentProvider, you can use
ContentResolver class to obtain ContentResolver
Object. You can use the getContentResolver () method provided by Activity.
ContentResolver uses insert, delete, update, and query methods to operate data. V. Summary: The storage methods in the above 5 are described. In the future development process, matching is found based on the design objectives, performance requirements, and space requirements.
Suitable for data storage. Android
The data storage in is private and cannot be accessed by other applications unless the data shared by other applications is obtained through ContentResolver. To share data externally by using files, you must
Perform file operations to read and write data. sharedpreferences is used to share data, and sharedpreferences must be used.
API reads and writes data. The advantage of using ContentProvider to share data is to unify the data access mode.
From: http://hi.baidu.com/maguowei/blog/item/7aca46c25574a33ae5dd3ba4.html