在webView android中加载本地html文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20873749/
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
Loading local html file in webView android
提问by donadev
I have to load an existing html file into a WebView
that is located at this path in the file system:
我必须将现有的 html 文件加载到WebView
文件系统中位于此路径的文件中:
/data/data/com.example.example/files/file.html
But, when the WebView
loads it, I don't see anything.
Who can help me?
但是,当WebView
加载它时,我什么也看不到。谁能帮我?
WebView
code (assuming path is the path I've written above):
WebView
代码(假设路径是我上面写的路径):
WebView webView = (WebView)findViewById(R.id.webView1);
File htmlFile = new File(path);
if(htmlFile.exists())
{
webView.loadUrl(htmlFile.getAbsolutePath());
}
回答by hichris123
Try this, adding in a file:///
and doing it a little differently:
试试这个,添加 afile:///
并做一些不同的事情:
WebView webView = (WebView)findViewById(R.id.webView1);
webview.loadUrl("file:///data/data/com.example.example/files/file.html");
Instead of this, however, you could just put the file into your assets
folder in the source code, and then do this:
但是,您可以将文件放入assets
源代码中的文件夹中,然后执行以下操作:
WebView webView = (WebView)findViewById(R.id.webView1);
webview.loadUrl("file:///android_asset/file.html");
回答by Nauman Ash
The html file should be placed in the assets folder, which will belong in the root directory of your project.
html 文件应该放在 assets 文件夹中,该文件夹将位于您项目的根目录中。
So move your file to in case of eclipse
因此,将您的文件移动到 eclipse 的情况下
assets/index.html
In an Android Studio project use this folder:
在 Android Studio 项目中使用此文件夹:
/app/src/main/assets/index.html
Now use
现在使用
WebView wv= (WebView)findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/index.html");
回答by M D
You need to implement a ContentProvider to map local files to uris as explained in this link how to display a local file into Android Webview
您需要实现一个 ContentProvider 以将本地文件映射到 uri,如此链接中所述如何将本地文件显示到 Android Webview
or you just load any html page from Assets folder like below:
或者您只需从 Assets 文件夹加载任何 html 页面,如下所示:
WebView wv= (WebView)findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/yourfile.html");
wv.getSettings().setJavaScriptEnabled(true);