Tracer69
Tracer69

Reputation: 1110

How to create a folder using Cordova on Android 10?

My Cordova application (Android) creates a folder (cordova-plugin-file) in the "Pictures" Directory, and stores an image into it. This works fine on Android 8, but I get the File-Error 12 ("The file or directory with the same path already exists." https://developer.mozilla.org/en-US/docs/Web/API/FileError) on Android 10.

// Get "/storage/emulated/0/" Folder
window.resolveLocalFileSystemURL(window.cordova.file.externalRootDirectory, dir => {
    console.log(dir) // This gets executed

    // Get / Create "Pictures" Folder
    dir.getDirectory("Pictures", { create: true, exclusive: false }, dir => {
        console.log(dir) // This also gets executed

        // Get / Create "Test" Folder
        dir.getDirectory("Test", { create: true, exclusive: false }, dir => {
            console.log(dir) // This doesn't

            // Create image
            dir.getFile(filename, { create: true }, file => {
                console.log(file)

                file.createWriter(fileWriter => {
                    fileWriter.write(blob)
                    resolve()
                }, console.error)
            }, console.error)
        }, console.error) // => FileError: 12
    }, console.error)
}, console.error)

When I change the line dir.getDirectory("Test", { create: true, exclusive: false } ... to dir.getDirectory("Test", { create: false } ..., I receive the FileError 1 ("A required file or directory could not be found at the time an operation was processed. For example, a file did not exist but was being opened."). So I tried to create the folder manually, and there was no error with this line anymore, but the image could not be created due to FileError 9 ("The modification requested is not allowed. For example, the app might be trying to move a directory into its own child or moving a file into its parent directory without changing its name.") My AndroidManifest.xml contains the line

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

so permissions shouldn't be a problem either. Keep in mind, that these errors do only occur on my Android 10 device, not the Android 8 one.

Does anyone have a clue what's going on here?

Upvotes: 3

Views: 1161

Answers (1)

Manouchehr Fazaeli
Manouchehr Fazaeli

Reputation: 11

Add this code to your config.xml file:

<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
  <application android:usesCleartextTraffic="true" />
  <application android:requestLegacyExternalStorage="true" />
</edit-config>

Upvotes: 1

Related Questions