Ionic 4, write file to android device

Hi,

I tried to use File imported from @ionic-native/file/ngx to write a .json file
to android device, I have followed the steps:

  1. Install it to my project

ionic cordova plugin add cordova-plugin-file
npm install @ionic-native/file

  1. add below to config.xml

  2. app.module.ts

import { File } from ‘@ionic-native/file/ngx’;

 providers: [
    StatusBar,
    SplashScreen,
    File
  ],

4. in my app.component.ts

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

storeData(){

var jsonString = JSON.stringify({
  ID: "Q1",
  Result1: "Yes",
  Result2: "No"});  
var fileDir = cordova.file.externalApplicationStorageDirectory; 
var filename = "result.json";
this.file.writeFile(fileDir, filename, jsonString, {replace: true}) ; 


}

What else I have not done or have done wrong?

Liying

What error does it show?

No error, I use console.log("after writeFile!’) and said the message, but
the file is not found in the device.

Have you looked into the directory of cordova.file.externalApplicationStorageDirectory?

According to Ionic documentation, the file should be saved to /Android/data/io.ionic.starter folder instead?

Yes, I recreated with the codes you provided, and it works just fine in my case, the result.json file is in my ~/Android/data/io.ionic.starter directory

So strange, maybe something wrong in the setting?
I used Android 6.0.1 on Nexus 5.

I have put below in config.xml

<preference name="AndroidPersistentFileLocation" value="Compatibility" />
<preference name="AndroidExtraFilesystems" value="files,cache, sdcard, cache-external, files-external" />

and my package.json:

“dependencies”: {
@angular/common”: “^7.2.2”,
@angular/core”: “^7.2.2”,
@angular/forms”: “^7.2.2”,
@angular/http”: “^7.2.2”,
@angular/platform-browser”: “^7.2.2”,
@angular/platform-browser-dynamic”: “^7.2.2”,
@angular/router”: “^7.2.2”,
@ionic-native/core”: “^5.0.0”,
@ionic-native/file”: “^5.6.1”,
@ionic-native/onesignal”: “^5.5.1”,
@ionic-native/splash-screen”: “^5.0.0”,
@ionic-native/status-bar”: “^5.0.0”,
@ionic/angular”: “^4.1.0”,
“cordova-android”: “7.1.4”,
“cordova-plugin-device”: “^2.0.2”,
“cordova-plugin-file”: “6.0.1”,
“cordova-plugin-ionic-keyboard”: “^2.1.3”,
“cordova-plugin-ionic-webview”: “^4.0.1”,
“cordova-plugin-splashscreen”: “^5.0.2”,
“cordova-plugin-statusbar”: “^2.4.2”,
“cordova-plugin-whitelist”: “^1.3.3”,
“core-js”: “^2.5.4”,
“onesignal-cordova-plugin”: “2.4.7”,
“rxjs”: “~6.5.1”,
“tslib”: “^1.9.0”,
“zone.js”: “~0.8.29”
},
“devDependencies”: {
@angular-devkit/architect”: “~0.13.8”,
@angular-devkit/build-angular”: “~0.13.8”,
@angular-devkit/core”: “~7.3.8”,
@angular-devkit/schematics”: “~7.3.8”,
@angular/cli”: “~7.3.8”,
@angular/compiler”: “~7.2.2”,
@angular/compiler-cli”: “~7.2.2”,
@angular/language-service”: “~7.2.2”,
@ionic/angular-toolkit”: “~1.5.1”,
@types/node”: “~12.0.0”,
@types/jasmine”: “~2.8.8”,
@types/jasminewd2”: “~2.0.3”,
“codelyzer”: “~4.5.0”,
“jasmine-core”: “~2.99.1”,
“jasmine-spec-reporter”: “~4.2.1”,
“karma”: “~4.1.0”,
“karma-chrome-launcher”: “~2.2.0”,
“karma-coverage-istanbul-reporter”: “~2.0.1”,
“karma-jasmine”: “~1.1.2”,
“karma-jasmine-html-reporter”: “^0.2.2”,
“protractor”: “~5.4.0”,
“ts-node”: “~8.1.0”,
“tslint”: “~5.16.0”,
“typescript”: “~3.1.6”
},
“description”: “An Ionic project”,
“cordova”: {
“plugins”: {
“onesignal-cordova-plugin”: {},
“cordova-plugin-whitelist”: {},
“cordova-plugin-statusbar”: {},
“cordova-plugin-device”: {},
“cordova-plugin-splashscreen”: {},
“cordova-plugin-ionic-webview”: {
“ANDROID_SUPPORT_ANNOTATIONS_VERSION”: “27.+”
},
“cordova-plugin-ionic-keyboard”: {},
“cordova-plugin-file”: {}
},

Do you have more or less these lines in your config.xml?

<allow-navigation href="*"/>
<allow-navigation href="http://*/*"/>
<allow-navigation href="file://*/*"/>
<allow-intent href="*"/>
<access origin="*"/>

I have those:

<access origin="*"/>
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />

I have added below to config.xml

<allow-navigation href="*"/>
<allow-navigation href="http://*/*"/>
<allow-navigation href="file://*/*"/>
<allow-intent href="*"/>

but still no file written.