使用 Cordova 文件插件创建文件时返回 INVALID_MODIFICATION_ERR

Aar*_*ron 6 android cordova

使用 Cordova 文件插件版本 1.3.3 在 Android5.1、Nexus 10、

  1. 遵循指南https://github.com/apache/cordova-plugin-file,并匹配此链接上的要求。
  2. 添加清单权限:android.permission.WRITE_EXTERNAL_STORAGE
  3. requestFileSystem接口返回的文件夹是“file:///storage/emulated/0/”,我们可以访问。
  4. 尝试了两个选项,得到相同的错误:

    首选项名称=“AndroidPersistentFileLocation”值=“内部”

    首选项名称="AndroidPersistentFileLocation" 值="兼容性"

示例代码:

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);});
Run Code Online (Sandbox Code Playgroud)

执行到接口 DirectoryEntry.prototype.getFile 时抛出错误 INVALID_MODIFICATION_ERR(9) 。

那么这个插件需要的任何其他配置可以在 Android 5.1 上运行吗?

顺便说一句,代码在 iPad 上正常工作。

小智 5

这个解决方案对我有用。将配置文件编辑为:

<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>
Run Code Online (Sandbox Code Playgroud)

在这里找到解决方案


小智 -1

确保在应用程序权限启用存储。 当应用程序尝试请求不存在的文件系统时抛出。INVALID_MODIFICATION_ERR