Intoxicated Penguin
Intoxicated Penguin

Reputation: 364

Trouble reading user accessible files

I am using nativescript-mediafilepicker as means of choosing a file, and this can read external storage successfully (I have downloaded a PDF to the 'downloads' folder on iOS and I am able to pick it.) I then try to load the file using the file system module from nativescript library, and this fails because it is listed as NativeScript encountered a fatal error: Uncaught Error: You can’t save the file “com.xxxxxx” because the volume is read only. This doesn't make sense as I am trying to read anyway - I don't understand where the saving part is from. The error comes from fileSystemModule.File.fromPath() line.

Something to note that file['file'] is file:///Users/adair/Library/Developer/CoreSimulator/Devices/82F397CE-B0B3-4ADD-AD52-805265C7AC49/data/Containers/Data/Application/7B47A8BD-6DBA-42CF-8792-38A8C5E61174/tmp/com.xxxxxx/test.pdf

Is the file automatically being pulled to an application specific directory after this media picker?

getFiles() {
      let extensions = [];
      if (app.ios) {
        extensions = [kUTTypePDF]; // you can get more types from here: https://developer.apple.com/documentation/mobilecoreservices/uttype
      } else {
        extensions = ["pdf"];
      }
      const mediaFilePicker = new Mediafilepicker();
      const filePickerOptions = {
        android: {
          extensions,
          maxNumberFiles: 1,
        },
        ios: {
          extensions,
          maxNumberFiles: 1,
        },
      };
      masterPermissions
        .requestPermissions([masterPermissions.PERMISSIONS.READ_EXTERNAL_STORAGE,masterPermissions.PERMISSIONS.WRITE_EXTERNAL_STORAGE])
        .then((fulfilled) => {
          console.log(fulfilled);
          mediaFilePicker.openFilePicker(filePickerOptions);
          mediaFilePicker.on("getFiles", function (res) {
            let results = res.object.get("results");
            let file = results[0];
            console.dir(file);
            let fileObject = fileSystemModule.File.fromPath(file["file"]);
            console.log(fileObject);
          });

          mediaFilePicker.on("error", function (res) {
            let msg = res.object.get("msg");
            console.log(msg);
          });

          mediaFilePicker.on("cancel", function (res) {
            let msg = res.object.get("msg");
            console.log(msg);
          });
        })
        .catch((e) => {
          console.log(e);
        });
    },

Upvotes: 2

Views: 151

Answers (1)

Intoxicated Penguin
Intoxicated Penguin

Reputation: 364

The issue I have experienced is resultant of the expectation of File.fromPath and what is returned by the file picker. File picker is returning a "file://path" URI, and File.fromPath is expecting a string of just "path".

Simply using the following instead is enough.

let fileObject = fileSystemModule.File.fromPath(file["file"].replace("file://","");

Upvotes: 3

Related Questions