SoFunction
Updated on 2025-03-04

Four application methods for Android data storage

Four application methods for Android data storage

As a complete application, data storage operations are essential. Therefore, the Android system provides a total of four data storage methods. They are: SharePreference, file storage, SQLite, and Content Provider. The differences and application scenarios of these methods are sorted out as follows.

The first: Use SharedPreferences to store data

Scope of application: Save a small amount of data, and the format of these data is very simple: string type, basic type value. For example, various configuration information of the application (such as whether to turn on the sound effects, whether to use vibration effects, player points for mini-games, etc.), unlocking password, etc.

Core principle: Save key-value pair data stored based on XML files, which is usually used to store some simple configuration information. Through the File Explorer panel of DDMS, expand the file browsing tree, it is obvious that SharedPreferences data is always stored in the /data/data//shared_prefs directory. The SharedPreferences object itself can only obtain data but does not support storage and modification. Storage modification is implemented through the internal interface Editor object obtained by (). SharedPreferences itself is an interface. The program cannot directly create a SharedPreferences instance. It can only obtain the SharedPreferences instance through the getSharedPreferences(String name, int mode) method provided by Context. In this method, the name represents the xml file name to be operated. The second parameter is as follows:

 Context.MODE_PRIVATE: Specify thisSharedPreferencesData can only be read by this application、Write。

Context.MODE_WORLD_READABLE: Specify thisSharedPreferencesData can be read by other applications,但不能Write。

Context.MODE_WORLD_WRITEABLE: Specify thisSharedPreferencesData can be read by other applications,Write

Compared with SQLite databases, SharedPreferences objects eliminate many operations such as creating databases, creating tables, writing SQL statements, etc., which is relatively more convenient and concise. However, SharedPreferences also has its own shortcomings, such as its function to store five simple data types: boolean, int, float, long and String, such as its inability to perform conditional query. So no matter how simple the data storage operation of SharedPreferences is, it can only be a supplement to the storage method and cannot completely replace other data storage methods such as SQLite databases.

The second type: file storage data

Files for saving data can be created in the storage device of the device itself or in an external storage device. Also under the default state, files cannot be shared among different programs.

Write a file: Call the () method to create a file based on the specified path and file name. This method will return a FileOutputStream object.

Read file: Call the() method to return a standard Java FileInputStream object through the established path and file name.

The third type: SQLite storage data

SQLite Database database. Android supports databases very well. It integrates SQLite databases and can be used easily for every application. Or rather, Android relies entirely on SQLite databases. All its system data and structured data are stored in the database. It has the following advantages: a. Excellent efficiency, which is undeniable b. Very suitable for storing structured data c. Easy to pass data between different activities and even different applications.

The fourth type: ContentProvider

In the Android system, a data storage method that can be shared by all applications. Since data is usually private between each application, this storage method is rarely used, but it is an indispensable storage method. For example, audio, video, pictures and address books can generally be stored in this way. Each ContentProvider will provide a public URI (wrapped into a Uri object). If the application has data to be shared, it is necessary to use the ContentProvider to define a URI for the data, and then other applications will pass in this URI through the Content Provider to operate on the data.

To sum up,Files are suitable for storing simple text or binary data, SharedPreferences are suitable for storing key-value pairs, and databases are suitable for complex relational data.

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!