0

im trying out some ionic native plugins, so far everything has been great excpet for the file plugin. I've been trying to create directory in the externalDataDirectory path provided by the file plugin. What am trying to do is check if the directory exists first but that actually seems to be the issue. Here is my code.

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

export class MyClass {

    constructor(private file: File, private filePath: FilePath) {}

    createDirectory(onSuccess?: (val: any) => any, onError?: (err: any) => any) {
    // root_dir = this.file.externalDataDirectory
    this.filePath.resolveNativePath(this.root_dir).then(
      (nativePath) => {
        this.file.checkDir(nativePath, 'myDir').then(
          (exists) => {
            if (!exists) {
              this.file.createDir(nativePath, 'myDir', false).then(
                (dirEntry) => {
                  onSuccess(dirEntry);
                },
                (err) => {
                  console.log('CreateDir error: ' + err);
                  onError(err);
                }
              ).catch( (exception) => { console.log(exception); } );
            }
            else {
              return nativePath + 'myDir';
            }
          },
          (err) => {
            console.log('CheckDir error: ' + err);
            onError(err);
          }
        ).catch( (exception) => { console.log(exception); } );
      },
      (err) => {
       // error occurs here.
        onError(err);
      }
    )
  }
}

I dont know what im doing wrong, can anyone be of assistance. Thanks!!!

Ralph Marvin
  • 149
  • 2
  • 8
  • Can you please post the error that you are getting? – Bruno Peres Apr 27 '17 at 00:12
  • Am getting an ENCODING_ERROR – Ralph Marvin Apr 28 '17 at 09:00
  • Looks like your error is caused by the line `this.filePath.resolveNativePath(...)`. Can you please confirm this? If true, can you please verify the value of `this.root_dir`? – Bruno Peres Apr 28 '17 at 11:22
  • The value of root_dir = file:///storage/sdcard0/android/data/com.ionicframework.myApp537218 – Ralph Marvin Apr 28 '17 at 12:14
  • According to [the docs](https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/#list-of-error-codes-and-meanings) ENCODING_ERROR is from File Plugin (and not File Path Plugin). So your error is in the line `this.file.createDir` or `this.file.checkDir`. Please inform the parameters value passed to these functions. – Bruno Peres Apr 28 '17 at 12:32

0 Answers0