Android data storage (Summary)

Source: Internet
Author: User
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:

  1. Use sharedpreferences to store data;
  2. File storage data;
  3. SQLite database stores data;
  4. Use contentprovider to store data;
  5. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.