使用 android WebView 加载现有的 .html 文件 [英] Loading existing .html file with android WebView

查看:37
本文介绍了使用 android WebView 加载现有的 .html 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实使用 WebView 尝试了来自 Google 代码的示例、演示和其他资源,但是当我尝试在自己的代码中执行此操作时,它对我不起作用.

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.

我想加载我放在assets文件夹中的myfile.html,并使用:

I want to load myfile.html which i put in assets folder, and using:

private WebView myWebView;

myWebView.loadUrl("file:///android_assets/myfile.html");

在模拟器上显示错误

位于 file:///android_assets/myfile.html 的网页无法加载为:未找到请求的文件./android_assets/myfile.html

The web page at file:///android_assets/myfile.html could not be loaded as: The requested file was not found. /android_assets/myfile.html

当我将该文件放入 res/raw/ 文件夹并使用:

When i put that file to res/raw/ folder and using:

myWebView.loadUrl("file:///android_res/raw/myfile.html");

那么可能只有模拟器 android 2.2 API 级别 8 可以加载文件,其他旧版本显示相同的错误.我错过了什么吗?

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?

是否有任何方法可以在适用于所有 API 版本的应用程序包中加载现有的 .html 文件?

Is there any way of loading an existing .html file in the application package which works on all API versions ?

推荐答案

好的,那是我非常愚蠢的错误.我在这里发布答案以防万一有人遇到同样的问题.

ok, that was my very stupid mistake. I post the answer here just in case someone has the same problem.

存储在assets文件夹中的文件的正确路径是file:///android_asset/*(assets文件夹没有s",我一直认为它必须有一个s").

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").

而且,mWebView.loadUrl("file:///android_asset/myfile.html"); 适用于所有 API 级别.

And, mWebView.loadUrl("file:///android_asset/myfile.html"); works under all API levels.

我仍然不明白为什么 mWebView.loadUrl("file:///android_res/raw/myfile.html"); 仅适用于 API 级别 8.但现在没关系.

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.

这篇关于使用 android WebView 加载现有的 .html 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆