This is the 7th day of my participation in the August More Text Challenge

preface

Local data cache management is an important part of App application. This is especially important when the application has functions such as draft box, audio and video cache, and important data saving. Once the data resources are mismanaged, it is likely to cause data loss, important data leakage and other problems, and it is likely to cause unnecessary trouble for version iteration update. Android provides four ways for developers to save application data: application-specific storage space, shared storage space, preferences, and database.

Application data saving mode

Application-specific storage space

Stores files that are only used by the application and are only used by the application. The files stored in the internal storage space can only be accessed by the application. They can be read and written externally and have privacy.

Shared Storage Space

Sharing storage space actually means that some resource data of an application can be accessed by other applications, including media, documents, and other files.

Key-value pair data

Key-value data preservation is the SP(SharedPreferences) in Android development. SharedPreferences Stores data resources in string-vaule format and supports String, int, float, Long, and Boolean common basic types. Save the data with SharedPreferences.Editor and get the data from SharedPreferences. SharedPreferences Saving data requires commit or apply. Commit is synchronous execution and should not block the main thread. Apply is asynchronous execution and should pay attention to thread safety.

 SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt("intVaule", newHighScore);
    editor.commit();
    editor.apply();
    sharedPref.getInt("intVaule".0);
Copy the code

The database

Using SharedPreferences for small file resources is a good solution to basic problems, but using SharedPreferences for large files and large amounts of data is a bit overwhelming. Therefore, big data and frequently read and written resources can be stored in the form of a database. Google officially launched Room database framework, Room based on SQLite provides an abstraction layer on its basis to facilitate database operation. For details about how to use the Room database, please refer to the official documents

In addition to the Realm cross-platform database framework, with higher encryption protection, faster, better use features; GreenDAO is also an excellent third-party open source database framework for Android.

storage

Data caching aside, application developers also need to know how to store data appropriately. For example, our application data hope to follow the application itself. For example, after uninstalling the application, the application data can be emptied in time without leaving any residual data (I take every cloud when I leave), which is the self-cultivation of a green application. Or when clearing the application cache during user operating system management, users can not clear important data that they want the application to save. So how and where to store application files is a matter for developers to think about and design.

In the old era of Android, many mobile phones use external SD cards for data storage due to their limited internal storage space. However, nowadays, mobile phones rarely use external SD cards to expand their storage space, and they have enough storage space. It can be said that now mobile phones have internal “SD card”, “SD card” is external storage, the phone itself has internal storage, which is also called storage partition.

Internal storage

In internal storage after every application installed on the android system has its own unique file directory, such as the app package name “com. Julyyu. Develop”, the name is the application of internal storage directory: / data/user / 0 / com. Julyyu. Develop /. Android native system supports multiple users, so there is a /user/0/ design, similar to Windows system to create a new home user, each user store data is independent, but Android mobile phone system is not used to multiple users (who will use the same phone at the same time).

As mentioned above, persistent storage content like SharedPreferences and database file resources are stored in internal storage to ensure the security of application data. Usually this data cannot be accessed directly through the mobile file manager, except through special means (root, high privilege, application developer).

In addition to the above two types of persistent storage, developers can also store files in internal storage in the following ways.

directory instructions
cache Cache file directory getCacheDir()
files File directory getFilesDir()
databases Database file
shared_prefs Sp storage

External storage

An external storage device is different from an internal storage device because internal applications can read and write data from internal storage devices. To access an external storage device, you need to grant read and write permissions for files.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Copy the code

External storage devices include SDcard and storage. In fact, both of them are mapped to the same directory. The external memory card sdcard directory should be reserved for historical reasons in the Android era.

sdcard storage

An application can create its own external storage directory on an external storage device. You can use the following methods to create directory files, such as the cache directory

The method name instructions
getExternalCacheDir() /storage/emulated/user/0/Android/data/com.julyyu.develop/cache/
getExternalFilesDir(Environment.DIRECTORY_PICTURES) /storage/emulated/user/0/Android/data/com.julyyu.develop/Picutres/
getExternalFilesDir(Environment.DIRECTORY_DCIM) /storage/emulated/user/0/Android/data/com.julyyu.develop/DCIM/
getExternalFilesDir(null) /storage/emulated/user/0/Android/data/com.julyyu.develop/files/

In addition can also through the Environment. External.getexternalstoragedirectory () method to get to the external storage root directory, but is already abandoned method in high version no longer apply.

Data cleaning

The internal and external cache files of the application will be cleared if the user actively cleans the cache. To clear all data, it is internal and external to all files in the application directory.

Reference & in-depth reading

  • Android partitions store FAQs
  • Get your head around Android storage!