Application data file saving of Android system programming introduction series

The persistence of data in applications, whether simple SharedPreferences or sqlitedatabase, essentially saves data to some type of file in the system. Therefore, you can directly use the java.io.file file file class to access data of any type.

After obtaining the object of the file class, you can use its related methods to perform related operations such as reading and writing files. This part belongs to the basic knowledge of Java language development or kotlin language development. I won't talk more. On the Android system, there are different ways to obtain file objects for various reasons, which will be introduced in this paper.

Before understanding the file storage of data, of course, we should first understand the division rules of Android system for hardware storage devices.

The partition space of Android system installed on general devices is defined as the internal storage space of Android system. Generally, the installation package of the application and the relevant data files after installation are saved in the internal storage space by default. The internal storage space divides different access intervals for different applications, which can be operated, while the system user cannot normally access the internal storage space, which effectively prevents the file security between applications.

The space of internal storage is often not too large, so it is not recommended that applications occupy a lot of space in internal storage. Therefore, the Android system defines the external storage space, and the files too large for the application can be stored in the external storage, such as the photos saved by the camera application. At the same time, external storage is accessible to system users, which is the storage space shown by file management applications.

In some hardware devices, such as older mobile phones, in order to increase the hardware storage space, sdcard will be used to increase the expandable storage area. This part of storage also belongs to the category of external storage space of the system.

When accessing the external storage, the application needs to apply for the read-write permission of the external storage, including android.manifest.permission.read_ EXTERNAL_ Storage and android.manifest.permission.write_ EXTERNAL_ STORAGE。

The so-called system level file is that this kind of file allows any application in the system to read and write access. The files in the external storage space are system level files.

Before Android 10, i.e. API 29, the external SD card of the mobile phone can be used as a system level file. In the application, you can use getexternalstoragedirectory() series static methods of android.os.environment environment class to obtain relevant file files in external storage, and use isexternalstorageemulated() series static methods to obtain information such as whether external storage is loaded or not.

Starting from Android 10, the system introduced the concept of partitioned storage. In applications with partitioned storage enabled, only application level files and media files can be accessed. Application level documents will be explained in detail below. The so-called media files are stored in external storage, mainly including pictures, audio, video and download files. There are four types of media files.

The general picture storage paths are DCIM / and picture /, and the file information is stored in the database marked by the relevant constants defined by the mediastore.images class; The audio storage paths are alarms /, audiobooks /, music /, notifications /, podcasts / and ringtones /, and the file information is stored in the database marked by the relevant constants defined by the mediastore.audio class; The video storage paths are DCIM /, movies / and picture /, and the file information is stored in the database marked by the relevant constants defined by the mediastore.video class; The download file storage path is download /, and the file information is stored in the database marked by the relevant constants defined by the mediastore.downloads class.

The opening method of partition storage is to modify the androidmanifest.xml manifest file and add the attribute value content Android: requestlegacyexternalstorage = "false" in the < Application > tag, which is also the default setting; On the contrary, if Android: requestlegacyexternalstorage = "true" is set, partition storage is turned off. It is officially recommended that this configuration can only be used as a transition in the file compatibility adaptation phase.

Applications with partition storage enabled do not need to apply for additional permissions when reading and writing their own application level files. However, when accessing media files, you also need access to external storage, including android.manifest.permission.read_ EXTERNAL_ Storage and android.manifest.permission.write_ EXTERNAL_ STORAGE。

The access method of media files is to obtain the android.content.contentresolver content resolution class object through the getcontentresolver () method of the context object. Through this object, you can add, delete, modify and query the Android system database. The relevant constants used can be queried in the above four media file classes. The principle and usage of contentresolver content parsing class will be explained in detail in the later article on application level file sharing.

In Android 11 and API 30 and above systems, partition storage is forcibly enabled, and applications can only access their application level files and media files. At the same time, the permission to manage external storage android.manifest.permission.manage is added_ EXTERNAL_ Storage, which is jointly authorized with the read-write permission of external storage above, allows the current application to access all files stored externally.

The same way to access external storage is to use getstoragedirectory () series static methods of environment class to obtain relevant file files in external storage.

The so-called application level file means that the file only runs the application to which it belongs for read-write access, can only be created by the application, and is deleted with the unloading or data clearing of the application.

Before the partition storage is turned on, that is, before the Android 10 system version, the application level files can be saved not only in the internal storage, but also in the external storage. Because the access to the external storage between applications is not limited, the application level files of the external storage can often be accessed by different applications.

Since Android 10 enabled partitioned storage, the application level files of the application can only be saved in the internal storage. Even if the access authorization of the application to the external storage is restored after Android 11 system, the application level files will not be created in the external storage.

The way to access application level files is through the context class android.content.context object.

Call the GETDIR (string name, int mode) method of the context object to obtain the private file in the specified directory and return the object of file file type. The usual path is / data / data / application package name / APP_ name/name。 Where parameter name is the specified file name; The parameter mode is the operation mode of the file, usually context.mode_ Private = 0 indicates that the file is private, or context.mode_ Append is the append mode for writing files. Call the getcachedir () method of the context object to obtain the cache file directory under the current application and return the object of file file type. The usual path is / data / data / application package name / cache /. Call the getfilesdir () method of the context object to obtain the specific file directory under the current application and return the object of file file type. The usual path is / data / data / application package name / files /. There are other related methods to obtain some specified file directories under the current application, which will not be repeated.

Application level files under an application are usually not allowed to be accessed by other applications, but what can be done if some application level files really want to be accessed by other applications, such as an address application that needs to access the contact information in the address book application? Please follow the next chapter for details.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>