Search code examples
javaandroidkotlinstorage

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


The official Google camera app (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 As you can see, it does not ask for that storage permission.


Solution

  • If you look at their 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
    }
    

    They are using getExternalMediaDirs() and, if none are available (which isn't ever the case in reality), getFilesDir() and both of those methods specifically mention in their documentation:

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