Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INVALID_MODIFICATION_ERR return when using Cordova file plugin create file

Using Cordova file plugin version 1.3.3 to create and write file on Android5.1, Nexus 10,

  1. Following the guide https://github.com/apache/cordova-plugin-file, and match what required on this link.
  2. Add permission to manifest: android.permission.WRITE_EXTERNAL_STORAGE
  3. Folder returned by requestFileSystem interface is "file:///storage/emulated/0/", we can access.
  4. Tried two options, get the same error:

    preference name="AndroidPersistentFileLocation" value="Internal"

    preference name="AndroidPersistentFileLocation" value="Compatibility"

Sample code:

var fileInfo = new Object();
fileInfo.createFile = true;
fileInfo.filename = "test.txt";
fileInfo.content = "test";
fileInfo.success = function(){alert("file Info success");};
fileInfo.error = function() {alert("file Info error");};

_write0 = function() {
    if(fileInfo.createFile) {
        var _write1 = function(downloadFolder) {
            theFileSystem.root.getFile(downloadFolder+"/"+fileInfo.filename,{create:true},function(f){
                f.createWriter(function(writer){
                    var _write2 = function() {
                        writer.onwriteend = function(evt) {
                            fileInfo.success();
                        };
                        var raw = atob(fileInfo.content);
                        var rawLength = raw.length;
                        var contentArray = new Uint8Array(new ArrayBuffer(rawLength));
                        for(var i=0;i<rawLength;i++) {
                            contentArray[i] = raw.charCodeAt(i);
                        }
                        var contentBlob = new Blob([contentArray.buffer]);
                        writer.write(contentBlob);
                    };

                    writer.onwriteend = function(evt){_write2();};
                    writer.onerror = function(evt){fileInfo.error();};
                    writer.truncate(0);
                });
            }, function(data){alert("Create File failed."); alert(data);});
        };

        _write1("File/myApp");
    }
};

requestFileSystem(PERSISTENT,1024*1024,function(fs){
    theFileSystem = fs;
    _write0();
},function(data){alert("requestFileSystem failed."); alert(data);});

Error INVALID_MODIFICATION_ERR(9) throws when executing into interface DirectoryEntry.prototype.getFile .

So any other configuration needed for this plugin worked on Android 5.1?

BTW, the code working correctly on iPad.

like image 862
Aaron Avatar asked Aug 19 '15 03:08

Aaron


1 Answers

This solution works for me. Edit the config file to this:

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

Found solution here

like image 129
Estudio 3551 Avatar answered Oct 05 '22 07:10

Estudio 3551