Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Where is the best place for android to cache data locally?
Where is the best place for android to cache data locally?
In the development of Android applications, we provide 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

The following is a description of the data storage methods in these areas:

First, use SharedPreferences to store data.

SharedPreferences is a lightweight storage class on the Android platform, which mainly stores some commonly used configurations, such as window state. Generally, the window state is overloaded in Activity and saved by SharedPreferences, which provides conventional long Integer, int and String saving on Android platform.

The second type: file stores data.

As for file storage, Activity provides the openFileOutput () method, which can be used to output data to a file. The specific implementation process is the same as saving data to a file in J2SE environment.

Files can be used to store a large amount of data, such as text, pictures, audio and so on.

Default location: /data/data/

The third type: SQLite database stores data

SQLite is a lightweight embedded database engine that supports SQL language.

And it only needs a little memory to have good performance.

In addition, it is open source and anyone can use it.

Many open source projects (Mozilla, PHP, Python) use SQLite.

SQLite consists of the following components: SQL compiler, kernel, backend and accessories.

By using virtual machine and virtual database engine (VDBE), SQLite makes it more convenient to debug, modify and extend the SQLite kernel.

The fourth method of using ContentProvider to store data, ContentProvider actually stores data through a database, so I won't describe it in detail here.

The fifth network stores data, which means that the data is stored in the server. On android, you only need to send a request through httpclient to get data from the server.

If the application needs to save data to the mobile phone during running, it will generally save the data in SDcard.

Most applications create a folder directly in the root directory of SDCard, and then save the data in this folder.

In this way, when the application is uninstalled, these data are left in the SDCard, leaving garbage data.

What if you want to uninstall your application and the data related to the application will be cleared?

The SDCard/Android/data/ package name of your application/file/directory can be obtained by the method of Context.getExternalFilesDir (), and generally some data that has been saved for a long time will be put.

SDCard/Android/data/ Your application package name/cache/directory can be obtained by the method of context. getExternalCache (), which generally stores temporarily cached data.

If the above method is used, after the application is uninstalled by the user, all files in the directory named SD card/Android/data/yourapplication/this will be deleted, and no spam will be left.