Android Storage

物理ストレージの場所を選択する 内部メモリのパーティションを外部ストレージとして割り当てているデバイスには、SD カードスロットも用意されていることがあります。つまり、その種のデバイスには外部ストレージを格納できる物理ボリュームが複数あります。したがって、アプリ固有のストレージに使用する物理ボリュームを選択する必要があります。

複数の場所にアクセスするには、ContextCompat.getExternalFilesDirs() を呼び出します。次のコード スニペットに示すように、返された配列の最初の要素がプライマリ外部ストレージ ボリュームと見なされます。空き領域がない場合またはボリュームを使用できない場合を除いて、このボリュームを使用します。

val externalStorageVolumes: Array<out File> =
        ContextCompat.getExternalFilesDirs(applicationContext, null)
val primaryExternalStorage = externalStorageVolumes[0]

https://developer.android.com/training/data-storage/app-specific#external

https://developer.android.com/reference/android/os/Environment?hl=ja#getExternalStoragePublicDirectory(java.lang.String)

getExternalStoragePublicDirectory Added in API level 8 Deprecated in API level 29

public static File getExternalStoragePublicDirectory (String type) Get a top-level shared/external storage directory for placing files of a particular type. This is where the user will typically place and manage their own files, so you should be careful about what you put here to ensure you don’t erase their files or get in the way of their own organization.

On devices with multiple users (as described by UserManager), each user has their own isolated shared storage. Applications only have access to the shared storage for the user they’re running as.

Here is an example of typical code to manipulate a picture on the public shared storage:

https://developer.android.com/reference/android/content/Context?hl=ja#getExternalFilesDir(java.lang.String)

https://developer.android.com/training/data-storage

https://developer.android.com/training/data-storage/use-cases#export-files-to-device

Export non-media files to a device Define a proper default location to store non-media files. Allow users to export files from app-specific directories to a more generally accessible location. Use MediaStore’s downloads or document collections to export non-media files to the device.

Note: To avoid cluttering, use generally accessible locations like externalStoragePublicDirectory() or externalMediaDirs().

https://developer.android.com/training/data-storage/shared/documents-files https://developer.android.com/training/data-storage/shared/documents-files#grant-access-directory

ACTION_OPEN_DOCUMENT_TREE 使う

https://qiita.com/nozaki-sankosc/items/686ac932420dcc82b063

Storage Access Framework #

https://developer.android.google.cn/guide/topics/providers/document-provider?hl=ja

https://developer.android.com/training/data-storage/shared/documents-files

fun openDirectory(pickerInitialUri: Uri) {
    // Choose a directory using the system's file picker.
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
        // Optionally, specify a URI for the directory that should be opened in
        // the system file picker when it loads.
        putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
    }

    startActivityForResult(intent, your-request-code)
}

Androidx.Activityを使用 #

https://developer.android.com/reference/kotlin/androidx/activity/result/contract/ActivityResultContract

https://developer.android.com/reference/kotlin/androidx/activity/result/contract/ActivityResultContracts.OpenDocumentTree

https://developer.android.com/reference/kotlin/androidx/activity/compose/package-summary#rememberlauncherforactivityresult

Composeの中で使用するには

val launcher = rememberLauncherForActivityResult(contract =
    ActivityResultContracts.OpenDocumentTree()) { uri: Uri? ->
        exportDir = uri

        if (exportDir != null) {
            Log.d("informationScreen", "selected uri: $exportDir")
        }
    }

https://developer.android.com/reference/androidx/documentfile/provider/DocumentFile