Android URL Scheme To Open File From Browser Link?

寵の児 提交于 2019-12-11 08:28:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!