cordova-plugin-filepath icon indicating copy to clipboard operation
cordova-plugin-filepath copied to clipboard

Different file paths returned depending on method of selection (Android 8)

Open ZaLiTHkA opened this issue 6 years ago • 9 comments

I've just noticed something strange about picking files with the "file chooser" that this launches on Android 8..

in this scenario, I'm trying to import a JSON file that I've downloaded, so naturally it's currently sitting in my device's "Download" folder. when the file chooser opens, by default the left sidebar has the usual source options like "Images", "Videos", "Audio" and "Recent". after this it shows "Downloads" and "Documents" before listing other apps that can be used to choose a file.

if I close this sidebar and open the kebab menu in the top right corner, I have an option to "Show internal storage". after this, I now see another option below "Documents" that lists my device's name, providing access to my device internal storage. in this route, I simply open the "Download" folder and I see the exact same files as the "Downloads" shortcut in the sidebar.

this is where things get interesting... I put some console.log calls at key points to see what was going on, thereby ending up with the following code:

this.fileChooser.open().then((uri) => {
  console.log(`fileChooser.open() result: "${uri}"`);
  return this.filePath.resolveNativePath(uri).then((fileEntry) => {
    console.log(`filePath.resolveNativePath(uri) result: "${fileEntry}"`);
    let path = fileEntry.substring(0, fileEntry.lastIndexOf("/"));
    let file = fileEntry.substring(fileEntry.lastIndexOf("/") + 1, fileEntry.length);
    console.log(`about to call readAsText() with: "${path}" "${file}`);
    return this.file.readAsText(path, file).then((content) => { ... });
  });
}).

as for the different ways of choosing the file, this is what I discovered:

when selected via "Downloads" sidebar shortcut:

  • fileChooser.open() result: "content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Fcustom-folder%2Fmy-custom-file.json"
  • filePath.resolveNativePath(uri) result: "file:///storage/emulated/0/Download/my-custom-file.json"
  • about to call readAsText() with: "file:///storage/emulated/0/Download" "my-custom-file.json

when selected via "browse internal storage":

  • fileChooser.open() result: "content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Fcustom-folder%2Fmy-custom-file.json"
  • filePath.resolveNativePath(uri) result: "file:///storage/emulated/0/Download/custom-folder/my-custom-file.json"
  • about to call readAsText() with: "file:///storage/emulated/0/Download/custom-folder" "my-custom-file.json

from what I can see, the path is fine up to the point that I use cordova-plugin-filepath to resolveNativePath().. so is this a bug in this plugin or am I simply misunderstanding something here?

ZaLiTHkA avatar Feb 26 '19 11:02 ZaLiTHkA

Hi

I've got the same problem. If fileChooser is used to open a file from downloads subfolder the subfolder is cut out. content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Fapptest%2F9f143ca0-7893-11e9-a410-87bb962a3e2a.pdf resolves to file:///storage/emulated/0/Download/9f143ca0-7893-11e9-a410-87bb962a3e2a.pdf

If I use the internal storage it works correctly.

zigapeda avatar May 29 '19 13:05 zigapeda

The same problem from @zigapeda the subfolder is been cut out from the result

"content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2FTESTE%2FOutroNome.docx"

Returns: "file:///storage/emulated/0/Download/OutroNome.docx"

afonsoUnivision avatar Jun 18 '19 17:06 afonsoUnivision

Is there any workaround for subfolder issue?

shlomir avatar Mar 01 '20 15:03 shlomir

Is there any workaround for subfolder issue?

I haven't found anything yet for this.. The app in which I discovered this behaviour simply includes a note in the user feedback message to suggest using a different "file browser" if file.readAsText fails to open the path returned from file.resolveLocalFilesystemUrl.

I've been working on other projects since then, so I haven't actually looked at this for a little while.. I still have a note of this in my backlog though, so when I get to this, if I do work out anything useful, I'll come post back here again.

ZaLiTHkA avatar Mar 02 '20 11:03 ZaLiTHkA

decodeURI(content://.../raw) worked for me before fetching the nativePath.

gaurav112 avatar Jul 13 '20 06:07 gaurav112

decodeURI(content://.../raw) worked for me before fetching the nativePath.

I have vague recollections of trying that previously, but I just tested again now and it doesn't make any difference.. decodeURI gives me the exact same URI as the string that comes from fileChooser.open().

interestingly, we seem to be experiencing what seems to be the same issue in iOS now, but I haven't had a chance to dig any more into that side yet.

ZaLiTHkA avatar Jul 20 '20 11:07 ZaLiTHkA

FWIW: I've abandoned this route of importing files from the local device.. I've replaced cordova-plugi-file-picker, cordova-plugin-file-chooser, and as a result, the usage of cordova-plugin-filepath, with cordova-plugin-chooser reading in the file Blob and decoding the data with TextDecoder... so, pretty much all of my example code in the OP has been replaced with two lines.

feel free to do whatever you want with this issue report, I'll be unsubscribing from notifications after posting this. :+1:

ZaLiTHkA avatar Jul 27 '20 08:07 ZaLiTHkA

Came across this issue just now. @hiddentao is there no possibility to get a fix?

graphefruit avatar Jul 31 '20 14:07 graphefruit

@graphefruit This package is no longer actively maintained. PRs are of course welcome.

hiddentao avatar Aug 01 '20 20:08 hiddentao