Why is cordova.file.documentsDirectory null?

2019-05-07 09:01发布

问题:

I am trying to use cordova-plugin-file-transfer at http://ngcordova.com/docs/plugins/fileTransfer/

The example code to specify the download location is as follows.

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

However, this property does not exist. I've logged console.log(JSON.stringify(cordova.file)) and I get the following output (formatted).

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

Furthermore, I've followed some hints from here https://cordova.apache.org/docs/en/dev/cordova-plugin-file/ and modified my config.xml with the following changes.

<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" />

This code is NOT running on an emulator or a browser, but a real android device. Any ideas on why this property is null?

When I attempt to download a file, all I get is an error. My code looks like the following.

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

This is the error (formatted).

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

Any ideas on what I'm doing wrong? Which cordova.file.* should I use if documentsDirectory is null?

回答1:

As far as i checked in cordova file plugin, cordova.file.documentsDirectory is not valid for Android.

This is the description mentioned in cordova file plugin official documentation in github :

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

Try out cordova.file.externalDataDirectory or cordova.file.externalRootDirectory for Android. Hope it helps