Cordova-plugin-filepath: Unable to resolve filesystem path

Trying to upload image from ionic application using cordova-plugin-camera via android/ios gallery. It’s working perfectly fine on ios but throw error while resolving path, I’m using cordova-plugin-filepath for resolving file path.

But it always throw following error while resolving native path in this.filePath.resolveNativePath(imagePath) method:

{code: 0 ; message: "Unable to resolve filesystem path."}

Here is my code for uploading image:

var options = {
  quality: 60,
  targetWidth:900,
  sourceType: sourceType,
  saveToPhotoAlbum: false,
  correctOrientation: true
};


// Get the data of an image
this.camera.getPicture(options).then((imagePath) => {
  if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
    console.log('image path',imagePath)
    this.filePath.resolveNativePath(imagePath)
      .then(res => {
        let correctPath = res.substr(0, res.lastIndexOf('/') + 1).toString();
        let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.length).toString();
        this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
      }).catch(err=>{
        console.log('unable to resolve file path issue', err)
      });
  } else {
    var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
    var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
    console.log(currentName,correctPath)
    this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
  }
}, (err) => {
  console.log(err);
});

I have even tried using following code but no success:

window.FilePath.resolveNativePath(imagePath)
      .then(res => {
        let correctPath = res.substr(0, res.lastIndexOf('/') + 1).toString();
        let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.length).toString();
        this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
      }).catch(err=>{
        console.log('unable to resolve file path issue', err)
      });

Here are my plugin details:

<plugin name="cordova-plugin-camera" spec="^4.0.3" />
<plugin name="cordova-plugin-filepath" spec="^1.4.2" />

Ionic info:

ionic (Ionic CLI) : 4.6.0
Ionic Framework : ionic-angular 3.9.2
@ionic/app-scripts : 3.2.1
Cordova:
cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
Cordova Platforms : android 7.1.4
Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 2.3.1, (and 15 other plugins)
System:
Android SDK Tools : 25.2.4
NodeJS : v9.11.1
npm : 6.0.1
OS : Windows 10"

your “/” path is incorrect, iOS uses a different path. Try:
“file:///”,"/"

Actually as i have already mentioned, my issue is with android upload from gallery, ios working perfectly fine.

hello i got this error on android 6.1 device. With the same code like you. It is really an android version problem or you have found anything else to solve that problem ?

I saw your last comment in stackoverflow.

Best regards

Its android version issue, for older version: above mentioned code does work but for latest android version i had to concatenate file:// with image path.

something like this:

this.camera.getPicture(options).then((imagePath) => {
imagePath = 'file://'+imagePath;
        console.log('image path',imagePath)
        this.filePath.resolveNativePath(imagePath)
          .then(res => {
            let correctPath = res.substr(0, res.lastIndexOf('/') + 1);
            let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.length).toString();
            this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
          }).catch(err=>{
            console.log('unable to resolve file path issue', err)
          });
}, (err) => {
    });
2 Likes

thanx bro awesome.you save my day .

bro please tell me where i pass key for sending image data , and how to add security coed key and many keys in this .

Hey!
I’m facing the same problem, but with Ionic 4 and Angular.
Sadly this solution doesn’t work for me :frowning:

  pickImage(sourceType) {
    const options: CameraOptions = {
      quality: 100,
      sourceType,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      correctOrientation: true,
      saveToPhotoAlbum: true
    };
    this.camera.getPicture(options).then((imagePath) => {
      imagePath = 'file://' + imagePath;
      console.log('image path', imagePath);
      this.filePath.resolveNativePath(imagePath)
        .then(res => {
          const correctPath = res.substr(0, res.lastIndexOf('/') + 1);
          const currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.length).toString();
          this.copyFileToLocalDir(correctPath, currentName, 'bg.jpg');
        }).catch(err => {
          console.log('unable to resolve file path issue', err);
        });
    }, (err) => {
    });
  }

does anybody have an idea or solution for this? in ionic 4?
I will post a question in the ionic-v4 forum aswell :slight_smile: