Android 在 WebView 中访问本地文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10620417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:23:59  来源:igfitidea点击:

Access local file in a WebView

androidwebview

提问by dongshengcn

My native app includes a WebView, and the WebView loads a web page off web. For example, the html is loaded from

我的本机应用程序包括一个 WebView,并且 WebView 加载了一个离网的网页。例如,html是从

http://dongshengcn.com/test.html

http://dongshengcn.com/test.html

It seems any page loaded from web (instead of local) can not load any file from local device.

似乎从网络(而不是本地)加载的任何页面都无法从本地设备加载任何文件。

My question is:

我的问题是:

Is it possible for a http://dongsheng.com/test.htmlloaded to a webview (as part of native app) to access file on local device?

是否可以将http://dongsheng.com/test.html加载到 webview(作为本机应用程序的一部分)来访问本地设备上的文件?

采纳答案by Theo

Here are a couple of things to try:

这里有几件事可以尝试:

  1. To use local files you need to place them in your project's assets folder and invoke them using URLs such as file:///android_asset/. For example, if you add mypage.html in your assets folder, then you can invoke it in the webview with file:///android_asset/mypage.html.

  2. Check to make sure that you have the appropriate webview permissions in your Manifest. For the webview to work correctly, you need:

    <uses-permission android:name="android.permission.INTERNET" />

  3. Take a look at the following app on Github, which as a bonus also fixes a couple of bugs with the webview in Honeycomb and ICS. It is a full example on how to use the webview with local files: https://github.com/bricolsoftconsulting/WebViewIssue17535FixDemo

  1. 要使用本地文件,您需要将它们放在项目的资产文件夹中,并使用诸如 file:///android_asset/ 之类的 URL 调用它们。例如,如果您在资产文件夹中添加 mypage.html,那么您可以在 webview 中使用 file:///android_asset/mypage.html 调用它。

  2. 检查以确保您在 Manifest 中拥有适当的 webview 权限。要使 webview 正常工作,您需要:

    <uses-permission android:name="android.permission.INTERNET" />

  3. 看看 Github 上的以下应用程序,作为奖励,它也修复了 Honeycomb 和 ICS 中的 webview 的几个错误。这是关于如何将 webview 与本地文件一起使用的完整示例:https: //github.com/bricolsoftconsulting/WebViewIssue17535FixDemo

EDIT: Addendum after question clarification:

编辑:问题澄清后的附录:

Yes, it is possible to load a local page from the web, but you must use a trick to bypass the browser's security measures.

是的,可以从 Web 加载本地页面,但是您必须使用技巧来绕过浏览器的安全措施。

Replace the file://android_asset/ portion of the URLs with a custom scheme (e.g. file///android_asset/mypage.html becomes myscheme:///mypage.html), and place these custom scheme URLs in your page. Implement WebViewClient's shouldOverrideUrlLoading, check if the URL begins with the custom scheme and if so redirect to the local page using webview.loadUrl.

将 URL 的 file://android_asset/ 部分替换为自定义方案(例如 file//android_asset/mypage.html 变为 myscheme:///mypage.html),并将这些自定义方案 URL 放置在您的页面中。实现 WebViewClient 的shouldOverrideUrlLoading,检查 URL 是否以自定义方案开头,如果是,则使用 webview.loadUrl 重定向到本地页面。

    mWebView.setWebViewClient(new WebViewClient()  
    {  
        @Override  
        public boolean shouldOverrideUrlLoading(WebView view, String url)  
        {  
            if (url != null && url.startsWith("myscheme://"))  
            {  
                String newUrl = url.replace("myscheme://", "file://android_asset/");  
                mWebView.loadUrl(newUrl);  
                return true;  
            }  
            return false;  
        }  
    }