Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

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

Open
ZaLiTHkA opened this issue Feb 26, 2019 · 9 comments

Comments

@ZaLiTHkA
Copy link

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?

@zigapeda
Copy link

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.

@afonsoUnivision
Copy link

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"

@shlomir
Copy link

shlomir commented Mar 1, 2020

Is there any workaround for subfolder issue?

@ZaLiTHkA
Copy link
Author

ZaLiTHkA commented Mar 2, 2020

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.

@gaurav112
Copy link

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

@ZaLiTHkA
Copy link
Author

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
Copy link
Author

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. 👍

@graphefruit
Copy link

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

@hiddentao
Copy link
Owner

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants