cordova-plugin-file icon indicating copy to clipboard operation
cordova-plugin-file copied to clipboard

All files reads gave a error code 5; ENCODING_ERR in latest release on Android

Open domingosl opened this issue 6 years ago • 13 comments

How to replicate the issue

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
    fs.root.getFile(cordova.file.applicationDirectory + "www/index.html", { }, function (fileEntry) {
       //DO NOTHING
    }, function(err) {
      console.log("error accesing the file", err);
    });

  }, function error(err) {
    console.log("error requesting the filesystem", err);
  });

This outputs:

error accessing the file FileError {code: 5}

The file exists, it is a Cordova project, and it also shows by doing:

function listDir(path){
  window.resolveLocalFileSystemURL(path,
    function (fileSystem) {
      var reader = fileSystem.createReader();
      reader.readEntries(
        function (entries) {
          console.log(entries);
        },
        function (err) {
          console.log(err);
        }
      );
    }, function (err) {
      console.log(err);
    }
  );
}

listDir(cordova.file.applicationDirectory + "www/index.html");

This shows that the file is present.

domingosl avatar Jan 16 '19 15:01 domingosl

I'm getting the same issue, except using Ionic. It works perfectly on iOS. cordova-plugin-file-opener2 2.1.4 and using com.android.support:support-v13:26.+ (not v4 due to a plugin conflict).

Opening the file:

this.fileOpener.open('file:///android_asset/www/assets/pdf/mypdf.pdf', 'application/pdf')
        .then(() => console.log('File is opened'))
        .catch(e => console.log('Error opening file', e));
// Error opening file {status: 9, message: "File not found"}

Checking that the file exists:

this.file.checkFile(path, filename)
        .then(result => console.log('File exists ', result))
        .catch(e => console.log('Problem checking file ', e));
// Problem checking file {code: 5, message: "ENCODING_ERR"}

Ionic info:

Ionic:

   ionic (Ionic CLI)  : 4.1.2
   Ionic Framework    : ionic-angular 3.9.2
   @ionic/app-scripts : 3.2.0

Cordova:

   cordova (Cordova CLI) : 8.1.2 ([email protected])
   Cordova Platforms     : android 7.1.4, ios 4.5.5
   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 1.2.1, (and 15 other plugins)

System:

   Android SDK Tools : 26.1.1 (~/Library/Android/sdk)
   ios-deploy        : 2.0.0
   NodeJS            : v8.12.0 (~/.nvm/versions/node/v8.12.0/bin/node)
   npm               : 6.4.1
   OS                : macOS
   Xcode             : Xcode 10.1 Build version 10B61

alex-steinberg avatar Feb 14 '19 08:02 alex-steinberg

Any solutions to this yet?

haroondilshad avatar Feb 15 '19 02:02 haroondilshad

@domingosl @alex-steinberg @RonCan Try this:

const justTheNameOfTheFile = "file.txt";
window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, async fs => {
      window.resolveLocalFileSystemURL(window.cordova.file.dataDirectory, async dirEntry => {
          dirEntry.getFile(justTheNameOfTheFile, { create: true, exclusive: false }, fileEntry => {
                 // what you want to do
            }
      });
});

It is because window.cordova.file.dataDirectory returns file:///data/user/0/... and .getFile checks if there is a : in the string and throws the encoding error then. But with the solution above it worked for me.

Benji1 avatar Mar 29 '19 10:03 Benji1

What version of this plugin and version of Android does this affect?

janpio avatar Jun 07 '19 13:06 janpio

I know github here is not for help, but can someone help here? https://stackoverflow.com/questions/56674724/apache-cordova-resolvelocalfilesystemurl-function-always-return-error-5-encod It's the same issue

jfoclpf avatar Jun 20 '19 21:06 jfoclpf

8 months later... always the same issue...

QuentinIcky avatar Aug 23 '19 13:08 QuentinIcky

hello this is 2020, works with ios sim, but not real device

crapthings avatar Feb 12 '20 12:02 crapthings

FYI I'm migrating my app to Capacitor, really enjoying it so far. It has removed all the headaches associated with Cordova. Most native functionality you'd need is native to Capacitor (like file system). Highly recommended.

alex-steinberg avatar Feb 12 '20 12:02 alex-steinberg

FYI I'm migrating my app to Capacitor, really enjoying it so far. It has removed all the headaches associated with Cordova. Most native functionality you'd need is native to Capacitor (like file system). Highly recommended.

Yeah...after Adobe running away, Cordova does not cut it anymore

mirko77 avatar Feb 04 '21 10:02 mirko77

Is Cordova dead? Seriously, their sqlite plugin is broken, I've been trying to write to a file all day, and nothing I try works. Not impressed.

fbl773 avatar Jun 17 '21 20:06 fbl773

Is Cordova dead? Seriously, their sqlite plugin is broken, I've been trying to write to a file all day, and nothing I try works. Not impressed.

Give Capacitor a try

mirko77 avatar Jun 17 '21 21:06 mirko77

@domingosl @alex-steinberg @RonCan Try this:

const justTheNameOfTheFile = "file.txt";
window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, async fs => {
      window.resolveLocalFileSystemURL(window.cordova.file.dataDirectory, async dirEntry => {
          dirEntry.getFile(justTheNameOfTheFile, { create: true, exclusive: false }, fileEntry => {
                 // what you want to do
            }
      });
});

It is because window.cordova.file.dataDirectory returns file:///data/user/0/... and .getFile checks if there is a : in the string and throws the encoding error then. But with the solution above it worked for me.

Hi guys i am having a error anybody how to fix ? thanks Asyc function not working, only work with this but i have a different error , i check permission but the problem apears is in android >10

window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, function() { window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory+"Pictures", function(dirEntry) { console.log(dirEntry); dirEntry.getFile("IMG_20210924_102017.jpeg", { create: true, exclusive: false }, function(fileEntry) { console.log(fileEntry); fileEntry.remove(function (file) {

      console.log("file removed!");
  }, function (error) {
      
      console.log("error occurred: " + error.code);
  }, function () {
      
      console.log("file does not exist");
  });
    })

}); }); But i have error 6 NO_MODIFICATION_ALLOWED_ERR: 6 error occurred: 6 image

necrodamus avatar Sep 24 '21 14:09 necrodamus

@domingosl @alex-steinberg @RonCan Try this:

const justTheNameOfTheFile = "file.txt";
window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, async fs => {
      window.resolveLocalFileSystemURL(window.cordova.file.dataDirectory, async dirEntry => {
          dirEntry.getFile(justTheNameOfTheFile, { create: true, exclusive: false }, fileEntry => {
                 // what you want to do
            }
      });
});

It is because window.cordova.file.dataDirectory returns file:///data/user/0/... and .getFile checks if there is a : in the string and throws the encoding error then. But with the solution above it worked for me.

What is the use of "fs" in second line? Do we really need it?

xs2coder avatar Nov 16 '21 12:11 xs2coder

getFile expects a filename not a file path.

It's intended (for either reading or creating) to get the inside the DirectoryEntry.

Please see the examples for valid usages.

breautek avatar Jan 25 '23 00:01 breautek