Google pay more and more attention to Android users privacy protection, so the Android Q version of the introduction of the concept of scope of storage, this operation directly restricts the developers to external storage is commonly used to do some configuration information for permanent storage way (actually developers because they do not comply with the development of specifications, result in the user’s mobile phone directory becomes cluttered).

Without further ado, I will conclude directly:

In-app directory:

  • GetCacheDir () = / data/user / 0 / packname/cache cache path of an application in internal storage

  • GetFilesDir () = / data/user / 0 / packname/files an application files in the internal storage path

  • GetExternalCacheDir () = / storage/emulated / 0 / Android/data/packname/cache an application cache path in the external storage

  • GetExternalFilesDir (” “) = / storage/emulated / 0 / Android/data/packname/files an application files in the external storage path

  • GetDir (” XXXXX “, MODE_PRIVATE). GetAbsolutePath () = / data/user / 0 / packname app_xxxxx custom path of an application in internal storage

Each of the above method paths contains a package name that indicates that it belongs to an application and can be accessed directly through File() without applying for read and write permission

External directory (deprecated on Android Q)

  • Environment. GetDataDirectory () = / data root of the internal storage
  • Environment.getDownloadCacheDirectory() = /data/cache
  • Environment.getRootDirectory() = /system
  • Environment. External.getexternalstoragedirectory (.) getAbsolutePath () = / storage/emulated / 0 external storage root
  • Environment. GetExternalStoragePublicDirectory (” “). GetAbsolutePath () = / storage/emulated / 0 external storage root

If the version of Android Q is later than Android Q, you can directly use File() to access the File when you have read and write permission. If the version of Android Q is later than Android Q, an exception is reported that the permission is insufficient.

Public shared directory

  • Image: MediaStore.Images.Media.EXTERNAL_CONTENT_URI
  • Video: MediaStore.Video.Media.EXTERNAL_CONTENT_URI
  • Audio: MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
  • Downloads: MediaStore.Downloads.EXTERNAL_CONTENT_URI

Android classifies file types. Images, audio, and video files can be accessed through the MediaStore API, while other types of files need to be accessed using the system’s file picker.

The image, audio, or video that an application contributes to the media library automatically has read and write permissions. You do not need to apply for the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions. If you want to read images, audio, or video that other applications contribute to the media library, you must apply for the READ_EXTERNAL_STORAGE permission. The WRITE_EXTERNAL_STORAGE permission will be deprecated in future Android versions.

How do I add data to a public shared directory

  • Insert: Image, video, audio, downloads can use contentResolver. The insert (uri, contentValues) method to get the uri of the new file, and then through contentResolver. OpenOutputStream (URI) Byte stream inserts data
try { val contentValues = ContentValues() contentValues.put(MediaStore.Downloads.DISPLAY_NAME, filename) contentValues.put(MediaStore.Downloads.DATE_TAKEN, System.currentTimeMillis()) val uri = this.contentResolver.insert( MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues ) val os = this.contentResolver.openOutputStream(uri!!) os? .use {os.write(" this is test data ".tobytearray ()) os.flush()}} Catch (e: Exception) {e.printStackTrace()}Copy the code

How do I obtain media files in a shared directory

  • GetUri: Image, video and audio can be through contentResolver. The query (uri, the projection, selection, selectionArgs, sortOrder) method to find the to the uri, and downloads the file only through the In Tent (intent.intent.action_open_document) file selector, in the onActivityResult() method through data.data to obtain the file uri
/ / retrieve Images in the album val cursor = contentResolver. The query (MediaStore. Images. Media. EXTERNAL_CONTENT_URI, null, null, null, "${MediaStore.MediaColumns.DATE_ADDED} desc") if (cursor ! = null) { while (cursor.moveToNext()) { val id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID)) val uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Id) println("image uri is $uri")} cursor.close() Intent(Intent.ACTION_OPEN_DOCUMENT).apply { addCategory(Intent.CATEGORY_OPENABLE) type = "*/*" putExtra( DocumentsContract.EXTRA_INITIAL_URI, MediaStore.Downloads.EXTERNAL_CONTENT_URI ) } startActivityForResult(intent, SELECT_REQ)Copy the code

How do I update existing media files in a public shared directory

  • Update: after get the uri of the file, through contentResolver. OpenOutputStream (uri) method, through the byte streams to change the file content
val os = this.contentResolver.openOutputStream(uri!!) os? .use {os.write(" this is test data ".tobytearray ()) os.flush()}Copy the code