为什么cordova.file.documentsDirectory为null?

Jan*_*yne 4 android angularjs cordova ionic-framework

我想在http://ngcordova.com/docs/plugins/fileTransfer/上使用cordova-plugin-file-transfer

指定下载位置的示例代码如下.

var targetPath = cordova.file.documentsDirectory + "testImage.png";
Run Code Online (Sandbox Code Playgroud)

但是,此属性不存在.我已经登录console.log(JSON.stringify(cordova.file)),我得到以下输出(格式化).

{
    "applicationDirectory": "file:///android_asset/",
    "applicationStorageDirectory": "file:///data/data/com.ionicframework.mobile379071/",
    "dataDirectory": "file:///data/data/com.ionicframework.mobile379071/files/",
    "cacheDirectory": "file:///data/data/com.ionicframework.mobile379071/cache/",
    "externalApplicationStorageDirectory": "file:///storage/emulated/0/Android/data/com.ionicframework.mobile379071/",
    "externalDataDirectory": "file:///storage/emulated/0/Android/data/com.ionicframework.mobile379071/files/",
    "externalCacheDirectory": "file:///storage/emulated/0/Android/data/com.ionicframework.mobile379071/cache/",
    "externalRootDirectory": "file:///storage/emulated/0/",
    "tempDirectory": null,
    "syncedDataDirectory": null,
    "documentsDirectory": null,
    "sharedDirectory": null
}
Run Code Online (Sandbox Code Playgroud)

此外,我已经从https://cordova.apache.org/docs/en/dev/cordova-plugin-file/中获得了一些提示,并通过以下更改修改了我的config.xml.

<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="iosPersistentFileLocation" value="Library" />
<preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" />
<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,root" />
Run Code Online (Sandbox Code Playgroud)

此代码不是在模拟器或浏览器上运行,而是真正的Android设备.关于为什么这个属性为null的任何想法?

当我尝试下载文件时,我得到的只是一个错误.我的代码如下所示.

$cordovaFileTransfer.download(url, path, {}, true)
    .then(function(result) { 
      console.log(JSON.stringify(result));
    }, function(err) { 
      console.log(JSON.stringify(err));
    }, function(progress) { 
      console.log(JSON.stringify(progress));
    });
Run Code Online (Sandbox Code Playgroud)

这是错误(格式化).

{
    "code": 1,
    "source": "http://192.168.0.169/api/user/file/e95cdcfd541746b3b0721c992d756137",
    "target": "IMG_20160224_030826.jpg",
    "http_status": 200,
    "body": null,
    "exception": "/IMG_20160224_030826.jpg: open failed: EROFS (Read-only file system)"
}
Run Code Online (Sandbox Code Playgroud)

关于我做错了什么的任何想法?cordova.file.*如果documentsDirectory为null,我应该使用哪个?

Gan*_*dhi 13

至于我在cordova文件插件中检查过,cordova.file.documentsDirectory对Android无效.

这是github中cordova文件插件官方文档中提到的描述:

"cordova.file.documentsDirectory - 专用于应用程序的文件,但对其他应用程序(例如Office文件)有意义.请注意,对于OSX,这是用户的〜/ Documents目录.(iOS,OSX)"

试用适用于Android的cordova.file.externalDataDirectory或cordova.file.externalRootDirectory.希望能帮助到你