Google 相机应用如何在不请求存储权限的情况下保存照片?

How does the Google camera app save photos without asking for a storage permission?

官方 Google 相机应用程序(https://github.com/android/camera-samples/tree/main/CameraXBasic) does not ask for any storage permissions. So, how does it save the photos to the device? just look at the AndroidManifest.xml file: https://github.com/android/camera-samples/blob/main/CameraXBasic/app/src/main/AndroidManifest.xml 如您所见,它不要求存储权限。

如果你看看他们的 getOutputDirectory method:

fun getOutputDirectory(context: Context): File {
    val appContext = context.applicationContext
    val mediaDir = context.externalMediaDirs.firstOrNull()?.let {
        File(it, appContext.resources.getString(R.string.app_name)).apply { mkdirs() } }
    return if (mediaDir != null && mediaDir.exists())
        mediaDir else appContext.filesDir
}

他们正在使用 getExternalMediaDirs() and, if none are available (which isn't ever the case in reality), getFilesDir(),并且在他们的文档中特别提到了这两种方法:

No additional permissions are required for the calling app to read or write files under the returned path.