Search code examples
javascriptcordovaionic-frameworkphonegap

How to use cordova-plugin-filepath?


I'm trying to output selected file's path in the DOM by using JS only. For that I'm using

https://github.com/ihadeed/cordova-filechooser & https://github.com/hiddentao/cordova-plugin-filepath

plugins

openFile: function() {
fileChooser.open({ mime: "audio/mpeg" }, app.winCallback, app.failCallback); winCallback: function() {
let actualPath;
let err;
fileChooser.open(function(uri) {
  window.FilePath.resolveNativePath(uri, actualPath, err);
  alert(actualPath);
}); } , failCallback: function() {
console.log("Couldn't access files"); }

I'm getting the selected file's URI, But I'm unable to understand how to use this with cordova-plugin-filepath.

I'm trying to get a file path something like this

file:///storage/emulated/0/planetes.mp3


Solution

  • The function has to structured in following way. This seems to work on Android 6. The fileChooser plugin didn't work on android 4.4.2.

     winCallback: function() {
    fileChooser.open(function(uri) {
      window.FilePath.resolveNativePath(uri, successNative, failNative);
      function failNative(e) {
        console.error("Something Went Wrong!");
      }
      function successNative(finalPath) {
        var path = finalPath;
        console.log(path);
        document.getElementById("audio-file").src = path;
      }
    }); }