Location>code7788 >text

Android Storage Overview

Popularity:936 ℃/2024-08-22 10:32:16

storage area

Android starts out by dividing the storage area intointernal storagecap (a poem)external storageThis corresponds to the phone's own storage and a pluggable sd card (analogous to a PC's hard drive and USB flash drive).

Internal storage capacity is limited, and Google recommends that app data be stored in external storage as much as possible.

As hardware technology evolved and phones with their own high-capacity space began to appear, descriptions of internal storage gradually drifted away from reality, and so from theAndroid 4.4(API 19)Beginning, the official body storage is no longer equated with internal storage, but logically assigns a part of it to external storage, limiting the capacity of the remaining part, which is now called internal storage. This operation allows the characteristics and usage scenarios of the original internal and external storage to continue.

Of course, if an sd card is inserted in a phone with 4.4 system and above, then the sd card is also considered external storage.

We can print out all the external storage using the following code:

File[] files;
if (.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    files = getExternalFilesDirs(Environment.MEDIA_MOUNTED);
    for(File file:files){
        ("main",file);
    }
}

For high-capacity phones above 4.4 with an sd card inserted, the following message should be printed:

/storage/emulated/0/Android/data/packname/files/mounted 
/storage/B3E4-1711/Android/data/packname/files/mounted 

file

Application-specific documents

Files that are for application use only can be stored in an application-specific directory in either internal storage or external storage, and the application does not need any permissions to access them. In terms of data security, although both are exclusive directories, internal storage can ensure that other applications can not access them, while external storage is more complicated.

On lower versions of Android, simply declare theREAD_EXTERNAL_STORAGEpermission to access any file located outside of the application's proprietary directory in external storage; just declare theWRITE_EXTERNAL_STORAGEpermission can then write to any file outside of the application's proprietary directory.

This is really quite dangerous and no one wants the data in their own app to be captured or tampered with. So fromAndroid 10(API 29)It's starting to happen.partitioned storageconcept, the app will by default be able to access its own proprietary directory on the external storage, as well as specific types of media files created by this app (using theMediaStore API(which will be discussed below). In this way, the application no longer needs to declare the above permissions, except in special cases.

At this point, if the application is requesting storage-related permissions at runtime, a request dialog (Dynamic Application) will pop up indicating that the application is requesting broad access to external storage.

Android 11(API 30)Start going a step further and simply wipe out the role of the WRITE_EXTERNAL_STORAGE permission (even if it is declared). This restricts the application's write permissions entirely to directories related to this application (proprietary directories and media files created by this application).

ps: Android 11 introduced the_MANAGE_EXTERNAL_STORAGE_permission, which replaces WRITE_EXTERNAL_STORAGE and provides write access to application-specific directories and files outside of the MediaStore, but has stricter usage requirements. For more information, see the documentation available atManage all files on the storage device

shared file

Store files that your app intends to share with other apps, including media (images, audio files, videos), other types of files.

media file

Accessed using the MediaStore API. Note: Even if your app is uninstalled, media files that are shared files (saved in the media library) remain on the user's device.

In addition to accessing your own media files, accessing other apps' media files requires permissions - in Android 11 (API 30) or later, READ_EXTERNAL_STORAGE; in Android 10 (API 29), READ_ EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE; in lower versions, access to all files requires the relevant permissions. --But that's not absolute.

for examplePhoto SelectorIt provides a navigable interface that offers users a secureBuilt-in authorizationway that allows the user to grant access to the app limited to selected photos and videos, rather than the entire media library, which is retained until the device is rebooted or the app stops running. The use of the photo selector can be viewed as a customized interface for dynamically requesting permissions from at least theAndroid 13(API 32)start without prior declaration of READ_EXTERNAL_STORAGE.

Other documents

Since Android 4.4 (API 19), there are officialStorage Access FrameworkThis framework facilitates application interaction with document providers, including external storage volumes and cloud storage. The framework allows users to interact with system selectors to choose document providers and specific documents and other files for your application to create, open, or modify.

Similar to the photo selector, the mechanism does not require any system privileges as the user is involved in selecting the files or directories your app can access, while user control and privacy protection are enhanced.

These files are stored outside of the app's proprietary directory and media library and remain on the device after the app is uninstalled.

Using the Storage Access Framework involves the following steps:

  1. The application calls the intent containing the storage-related operation (ACTION_CREATE_DOCUMENTSave the file;ACTION_OPEN_DOCUMENTOpen the file;ACTION_OPEN_DOCUMENT_TREE(granting the application access to all files and subdirectories in the directory).
  2. The user sees a system selector for browsing the document provider and selecting the location or document where the storage-related action will be performed.
  3. The application gains read and write access to a URI that represents a location or document selected by the user. Using this URI, the application can perform actions at the selected location.

digital

Application Configuration Items

Without further ado, it's simple key-value pairs. It's worth noting that previously it was all done using theSharedPreferencesCarrying out the operation of application configuration items is now officially recommended using theJetpack DataStoreDataStore allows you to use protocol buffers to store key-value pairs or typed objects.Kotlin programmerscap (a poem)Store data in asynchronous, consistent transactions.

comprehensive database

on the basis ofSQLiteThe data storage, which is generally chosenThis semi-ORM simplifies data CRUD operations. The database is deleted when the application is uninstalled.