Introduction of partitioned storage

The reason; To allow users to better manage their files and reduce clutter

Internal storage:

Context.getcachedir (); Data storage path/user / 0 / package/cache, capacity is small, when the system internal storage space is small, have been delete system risk

External storage:

1. External private storage access method Context. GetExternalFilesDir () path: / storage/sdcrad0 / Android/data/package/files

2. The external public path storage through the MediaStore add and delete, insert the default path/storage/sdcrad0 / Pictures, You can specify relative paths to environment. DIRECTORY_DCIM,Environment.DIRECTORY_MOVIES, etc.

Change of access rights

An application that uses partitioned storage always has read/write permission on the files it creates, regardless of whether the files are in the application’s private directory. Therefore, if your application only stores and accesses files it creates, you do not need to request READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permissions. However, to access files created by other applications, the following two conditions must be met: Your application has the READ_EXTERNAL_STORAGE permission.

These files are located in one of the following well-defined media collections:

Photos: Stored in Mediastore.images.

Video: Stored in mediastore.video.

Audio files: Stored in mediastore.audio.

How do I access mediastore.download, etc? Must use the storage access framework problem, what if you want to access the external storage private directory of another application?

File storage mode

1. Public directory of external files to determine whether SDcard can be stored:

fun isExternalStorageWritable(): Boolean {
    return Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED
}
fun isExternalStorageReadable(): Boolean {
     return Environment.getExternalStorageState() in
        setOf(Environment.MEDIA_MOUNTED, Environment.MEDIA_MOUNTED_READ_ONLY)
}
Copy the code

2. Save photos, audio files, or video clips to the external file public directory

MedieStore. Image. Media. InsertImage: generates thumbnails, ContentResover. Insert (URI, contentValues): applies to pictures, video and audio ACTION_CREATE_DOCUMENTintent: applies to PDF, other formats, and is part of the storage access framework

3. The external private directory: val getexternalfiledir access to the path the file = file (content. getexernalFiledir (null), Enviorment. DIRECTORY_DCIM)

4. File read and write: Android Q used to use streams, q uses file descriptors:

Common reading and writing:

fun write(file: File, any: Any, context: Context) { val descriptor = getDescriptor(file, context) ? : return ObjectOutputStream(FileOutputStream(descriptor.fileDescriptor)).safeUse { it.writeObject(any) } descriptor.safeClose() } fun readString(file: File, context: Context): String? { val descriptor = getDescriptor(file, context) ? : return null FileInputStream(descriptor.fileDescriptor).safeUse { return it.readString() } descriptor.safeClose() return null } private fun getDescriptor(path: File, context: Context): AssetFileDescriptor? { val uri = Uri.parse(file.absolutePath) return context.contentResolver.openAssetFileDescriptor(uri, "r") }Copy the code