cordova成功错误TypeError:无法读取null的属性'filesystemName'

时间:2017-06-26 13:03:48

标签: android cordova cordova-plugin-file

我有一个应用,其中以下逻辑在每次第二次时在我在Android上运行应用时失败:

 window.requestFileSystem( LocalFileSystem.PERSISTENT, 0, 
                    function( fs ) {
                      console.log('Success!', fs);
                    }, 
                    function( e ) {
                        console.error('Fail!', e);
                    }
                );

我得到的错误是:

  

cordova.js:312成功回调calldId:File1306990920:   TypeError:无法读取null

的属性'filesystemName'

Cordova.js第312行是callbackFromNative函数中的通用catch。

有没有人碰到过这个?

补救措施是什么?

1 个答案:

答案 0 :(得分:0)

<强>更新

只需从config.xml中删除<preference name="AndroidPersistentFileLocation" value="Compatibility" />,或者为"Compatibility"更改"Internal",似乎有责任将空值添加到数组响应中,如下所述

我正在使用带有Ionic2的cordova文件插件,如果不是你的情况,也许这会给你一些亮点。

我对此错误的描述将我带到.../plugins/cordova-plugin-file/www/fileSystems-roots.js,即fsName的地图 - &gt;文件系统。

这让我认为这是一个错误,但我不确定如何报告它。

无论如何,问题是对map的响应包含一个null对象,你可以在下面的响应副本中看到:

[
    null
    , {"fullPath":"/","filesystemName":"persistent","isDirectory":true,"nativeURL":"file:...","filesystem":1,"isFile":false,"name":""}
    , {"fullPath":"/","filesystemName":"content","isDirectory":true,"nativeURL":"content:...","filesystem":1,"isFile":false,"name":""}
    , {"fullPath":"/","filesystemName":"assets","isDirectory":true,"nativeURL":"file:...","filesystem":1,"isFile":false,"name":""}
    , {"fullPath":"/","filesystemName":"cache","isDirectory":true,"nativeURL":"file:...","filesystem":1,"isFile":false,"name":""}
]

同样,我正在使用ionic2,如果也是你的情况,你可以直接使用File插件,按照下面的步骤:

0-安装插件(如果您收到此错误,我想您已经完成了此步骤)

1-在您的模块中将插件作为提供者导入,例如

import { File } from '@ionic-native/file';

@NgModule({
    ...
    , providers: [
        ...
        , File
        ...
    ]
    ...
})

2-在组件中添加插件作为提供程序

import { File } from '@ionic-native/file';

export class FileCreatorComponent {
    constructor(..., private file: File, ...) { }

3-根据此页Ionic2 Native file plugin使用它,例如创建(并覆盖,如果存在),您可以这样做:

createFile() {
    var filePath= this.file.externalRootDirectory + 'Download/';
    //var filePath= 'file:///storage/emulated/0/Download/'; If the first doesn't work you can put this path, NOT RECOMMENDED, because the first should work
    var fileName= 'newOne.txt';

    this.file.writeFile(filePath, fileName, 'Lorem ipsum', { replace: true } )
                .then(() => {
                    console.log('File created');
                });
}