cordova-plugin-file: Cordova 12 can not save file to Android API 33 file system

Bug Report

Problem

I am using Cordova 12.0 and I am creating cordova android 12 project API 33 on which I am trying to create text file and save it on phone memory so it could be then used vie phone File Explorer. I use simple code like code below that and createFile method itselfs simple uses fileWriter write to write Blob object to file.

         function readFile(fileEntry) {

            fileEntry.file(function (file) {
                var reader = new FileReader();

                reader.onloadend = function() {
                    alert("Successful file read: " + fileEntry.fullPath + ": " + this.result);
                };

                reader.readAsText(file);

            }, fail);
        }

        function writeFile(fileEntry, dataObj) {
            fileEntry.createWriter(function (fileWriter) {

                fileWriter.onwriteend = function() {
                    alert("Successful file write...");
                    readFile(fileEntry);
                };

                fileWriter.onerror = function (e) {
                    alert("Failed file write: " + e.toString());
                };

                if (!dataObj) {
                    dataObj = new Blob(['some file data'], { type: 'text/plain' });
                }

                fileWriter.write(dataObj);
            });
        }

        function createFile(dirEntry, fileName, isAppend) {
            dirEntry.getFile(fileName, {create: true, exclusive: false}, function(fileEntry) {

                writeFile(fileEntry, null, isAppend);

            }, fail);
        }

        window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory , function (dirEntry) {
            createFile(dirEntry, "fileToAppend.txt", false);
        }, fail);`

`

However I have no error but file has never been created in the device/emulator.

What is expected to happen?

File to be created somewhere in the device. Maybe I have to use different key than externalDataDirectory ?

What does actually happen?

Nothing

Environment, Platform, Device

Windows 10 running Android Studio

Version information

Cordova 12 Cordova android 12 Cordova-plugin-file 8.0.1 Android Emulator API 33

Checklist

  • I searched for existing GitHub issues
  • I updated all Cordova tooling to most recent version
  • I included all the necessary information above

About this issue

  • Original URL
  • State: open
  • Created 8 months ago
  • Comments: 22 (7 by maintainers)

Most upvoted comments

Yep @tmishutin , I was just able to make use of this plugin as well a couple of days ago.

const ReadWriteFilesDeviceMediaStore = async (stringData, fileName) => {
   const base64Data = btoa(unescape(encodeURIComponent(stringData)))
   const result = await cordova.plugins.safMediastore.writeFile({ "data": base64Data, "filename": fileName })
   return result;
}

ReadWriteFilesDeviceMediaStore('string for export', 'fileName.txt').

I know this is still a problem with cordova-plugin-file. After 4 days digging into the problem, the only solution that works stable so far is using another plugin: https://github.com/customautosys/cordova-plugin-saf-mediastore

here is an example on how to read file:

  window.cordova.plugins.safMediastore
      .readFile(uri)
      .then(arrayBuffer => {
        const blob = new Blob([new Uint8Array(arrayBuffer)], {
          type: file.type
        });
        resolve(blob);
      })
      .catch(error => {
        console.error("safMediastore Error reading file:", error, JSON.stringify(error));
        reject(error);
    });

@breautek sure, my initial post has been updated with the whole demo code for proof of concept to write and save to file.