问题
On Android (8.0), I'm trying to create a local html file that includes links to some local files that can't be viewed directly by Chrome, like <a href="relative_path_to_file/file.docx">
. In a normal web browser (i.e. on a PC), I can click the link and it will open the file in its default viewer. On Android, if I open one of the local html files in Chrome and click one of those links, it directs me to content://nextapp.fx.FileProvider/path_to_file/file.docx, showing "Your file was not found." I do have an appropriate app installed to view the file (I can open it via a file explorer app). I would like to understand how I can structure the URLs such that docs of various types (i.e. docx, xlsx, pdf, etc) can be accessed via the links that refer to them - clicking the link should offer to open the file, as it does when accessing the file from a file manager.
- I tried rewriting the links to an absolute path, i.e. "/storage/emulated/0/path/file.docx." The result is the same.
- I tried rewriting the links to "file:///storage/emulated/0/path/file.docx." Chrome just redirects to about:blank.
- I tried opening the html doc with "HTML Viewer" rather than Chrome. The behavior is more or less the same (in the first case it redirects to about:blank, in the second it says "No app available to open link.")
How can I structure the link so it can be clicked & open the referred-to file in appropriate viewer?
回答1:
Finally figured it out. A working link to open the local file in its default viewer looks like:
"intent:///abs/path/to/file.docx#Intent;scheme=file;action=android.intent.action.VIEW;end;"
...With one important caveat: it breaks if there are any "dots" in the path (i.e. I had the files in a .hiddenFolder, which apparently causes Chrome to be unable to figure out the intent link).
Reference: https://developer.chrome.com/multidevice/android/intents
来源:https://stackoverflow.com/questions/50111170/android-url-scheme-to-open-file-from-browser-link