Search code examples
androidandroid-storagescoped-storage

Android 10 storage ENOENT (No such file or directory)


private fun createOutputFile(fileNumber: Int): File {
    val directoryPath =
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
            .toString() + File.separator + FOLDER_NAME

    val file = File(directoryPath)
    if (!file.exists()) {
        file.mkdir()
    }

    val outputPath = directoryPath + File.separator + createFileName(fileNumber)

    return File(outputPath)
}

This code working on Android 11 and Android 9.

On Android 10 it throws open failed: ENOENT (No such file or directory). With android:requestLegacyExternalStorage="true" it works.

I want to get rid of requestLegacyExternalStorage, can someone explain why it is works on Android 11 and not works on Android 10? What is the difference?

Permission WRITE_EXTERNAL_STORAGE is granted.


Solution

  • I want to get rid of requestLegacyExternalStorage

    No. No. No.

    Why would you?

    There is no alternative.

    Leave it!

    It is needed for 10 and only used for 10 devices.

    You have to put it in manifest if you want to use that code on an Android 10 device.