使用 android WebView 加载现有的 .html 文件
我确实使用 WebView
尝试了来自 Google 代码和其他资源的示例、演示,但是当我尝试在自己的代码中执行此操作时,它对我不起作用。
我想加载我放在资产文件夹中的 myfile.html
,并使用:
private WebView myWebView;
myWebView.loadUrl("file:///android_assets/myfile.html");
在模拟器上显示错误
无法访问
file:///android_assets/myfile.html
的网页 加载为:找不到请求的文件。/android_assets/myfile.html
当我将该文件放入 res/raw/
文件夹并使用:
myWebView.loadUrl("file:///android_res/raw/myfile.html");
那么只有模拟器 android 2.2 API level 8 可能可以加载该文件,其他较旧的文件版本显示相同的错误。我错过了什么吗?
有没有什么方法可以加载适用于所有 API 版本的应用程序包中的现有 .html 文件?
I did try samples, demos from Google codes and other resources with WebView
, but when i try to do it in my own code, it doesn't work for me.
I want to load myfile.html
which i put in assets folder, and using:
private WebView myWebView;
myWebView.loadUrl("file:///android_assets/myfile.html");
On emulator shows error
The web page at
file:///android_assets/myfile.html
could not be
loaded as: The requested file was not found./android_assets/myfile.html
When i put that file to res/raw/
folder and using:
myWebView.loadUrl("file:///android_res/raw/myfile.html");
then only emulator android 2.2 API level 8 can load the file probably, other older versions show the same error. Am i missing something?
Is there any way of loading an existing .html file in the application package which works on all API versions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧,那是我非常愚蠢的错误。我将答案发布在这里,以防万一有人遇到同样的问题。
存储在资产文件夹中的文件的正确路径是
file:///android_asset/*
(资产文件夹没有“s”,我一直认为它必须有一个“s”)。并且,
mWebView.loadUrl("file:///android_asset/myfile.html");
适用于所有 API 级别。我仍然不明白为什么
mWebView.loadUrl("file:///android_res/raw/myfile.html");
仅适用于 API 级别 8。但现在已经不重要了。ok, that was my very stupid mistake. I post the answer here just in case someone has the same problem.
The correct path for files stored in assets folder is
file:///android_asset/*
(with no "s" for assets folder which i was always thinking it must have a "s").And,
mWebView.loadUrl("file:///android_asset/myfile.html");
works under all API levels.I still not figure out why
mWebView.loadUrl("file:///android_res/raw/myfile.html");
works only on API level 8. But it doesn't matter now.如果你的结构应该是这样的:
/assets/html/index.html
/assets/scripts/index.js
/assets/css/index.css< /code>
然后就做( Android WebView:处理方向更改 )
确保添加
然后只使用 url
If your structure should be like this:
/assets/html/index.html
/assets/scripts/index.js
/assets/css/index.css
Then just do ( Android WebView: handling orientation changes )
Make sure you add
Then just use urls
将 .html 文件粘贴到项目文件夹的 asset 文件夹中。
并使用以下代码在布局文件夹中创建一个 xml 文件:
my.xml:
在活动中添加以下代码
paste your .html file in assets folder of your project folder.
and create an xml file in layout folder with the fol code:
my.xml:
add fol code in activity
将 .html 文件复制并粘贴到项目的 asset 文件夹中,并在 onCreate() 的 Activity 中添加以下代码。
Copy and Paste Your .html file in the assets folder of your Project and add below code in your Activity on onCreate().
调试编译与发布编译不同,因此:
请考虑您的项目文件结构,如下所示[这种情况是针对调试汇编]:
您应该将 index.html 调用到您的 WebView 中,如下所示:
因此,对于发布组装,它应该类似于:
The debug compilation is different from the release one, so:
Consider your Project file structure like that [this case if for a Debug assemble]:
You should call index.html into your WebView like:
So forth, for the Release assemble, it should be like:
您可以手动读取 html 文件,然后使用 WebView 的
loadData
或loadDataWithBaseUrl
方法来显示它。You could read the html file manually and then use
loadData
orloadDataWithBaseUrl
methods of WebView to show it.