android - 为什么 cordova.file.documentsDirectory 为空?

标签 android angularjs cordova ionic-framework

我正在尝试在 http://ngcordova.com/docs/plugins/fileTransfer/ 处使用 cordova-plugin-file-transfer

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

var targetPath = cordova.file.documentsDirectory + "testImage.png";

但是,这个属性不存在。我记录了 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
}

此外,我还遵循了此处的一些提示 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" />

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

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

$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));
    });

这是错误(格式化)。

{
    "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)"
}

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

最佳答案

据我检查 cordova 文件插件,cordova.file.documentsDirectory 对 Android 无效。

这是github中cordova file plugin官方文档中提到的描述:

"cordova.file.documentsDirectory - Files private to the app, but that are meaningful to other application (e.g. Office files). Note that for OSX this is the user's ~/Documents directory. (iOS, OSX)"

试用适用于 Android 的 cordova.file.externalDataDirectory 或 cordova.file.externalRootDirectory。希望对你有帮助

关于android - 为什么 cordova.file.documentsDirectory 为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36367973/

相关文章:

android - 哪个卷轴最适合安卓 : PhoneGap

javascript - Phonegap + Sencha + Android : ajax request with a basic access authentication failled

android - android中如何检测用户触摸交互?

Android NumberPicker OnValueChangeListener

javascript - 从数组中提取值列表,消除重复

javascript - 如何在同一个 Angular JS 应用程序中使用具有不同身份验证 header 的不同 API

javascript - 如何将带有双引号反斜杠的 JSON 字符串转换为 Javascript 对象

cordova - Apache cordova Build 实现 firebase 失败 : Dependency failing

java - 在Android中获取 map 地址或位置地址

java - libGDXcamera.unproject() 返回 NaN