Java 在Webview-Android中打开本地Html文件
我在根文件夹中保存了一个文件,正在尝试在webview中打开它 这是我的保存代码:Java 在Webview-Android中打开本地Html文件,java,android,Java,Android,我在根文件夹中保存了一个文件,正在尝试在webview中打开它 这是我的保存代码: OutputStream outstream = null; outstream = openFileOutput(fileName ,MODE_WORLD_READABLE); /// if file the available for writing if (outstream != null) { /// prepare the file for writi
OutputStream outstream = null;
outstream = openFileOutput(fileName ,MODE_WORLD_READABLE);
/// if file the available for writing
if (outstream != null) {
/// prepare the file for writing
OutputStreamWriter outputreader = new OutputStreamWriter(outstream);
BufferedWriter buffwriter = new BufferedWriter(outputreader);
/// write the result into the file
buffwriter.write(result);
}
/// close the file
outstream.close();
} catch (java.io.FileNotFoundException e) {
System.out.println("File not found in the writing...");
} catch (IOException e) {
System.out.println("In the writing...");
}
这是我调用文件的代码:
fileView.getSettings().setJavaScriptEnabled(true);
fileView.loadUrl("file:///" + name); <---
fileView.getSettings().setJavaScriptEnabled(true);
加载URL(“文件://”+名称) 假设未命中异常,则路径是错误的
file:///告诉浏览器查找/name
openFileOutput(fileName)告诉应用程序写入()+文件.separator+文件名
WebView mWebView=(WebView)findViewById(R.id.mWebView);
mWebView.loadUrl("file:///book.html");
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSaveFormData(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient
{
@Override
//show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl (url);
return true;
}
}
当您使用file:///...
然后,这意味着您应该将文件保存在assets目录下(比如test.html)。
现在假设您必须访问test.html文件,您需要这样编写
loadURL("file:///android_asset/test.html');
BufferedReader read = null;
StringBuilder data = new StringBuilder();
try {
read = new BufferedReader(new InputStreamReader(getAssets().open("htmlFile.html"), "UTF-8"));
String webData;
while ((mLine = read.readLine()) != null) {
data.append(mline);
}
} catch (IOException e) {
log(",e.getmessage()) } finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
log(",e.getmessage())
}
}
}
对于将与应用程序捆绑的文件,您可以在项目浏览器中右键单击应用程序,然后选择
新建=>文件夹=>资产文件夹
将HTML文件添加到资产文件夹,然后通过以下方式加载:
fileView.loadUrl("file:///android_asset/"+name);
可以在HTML中使用相同的URL链接到其他HTML或CSS文件。您可以先读取资产文件,然后通过asd将其显示在webview上
像这样
loadURL("file:///android_asset/test.html');
BufferedReader read = null;
StringBuilder data = new StringBuilder();
try {
read = new BufferedReader(new InputStreamReader(getAssets().open("htmlFile.html"), "UTF-8"));
String webData;
while ((mLine = read.readLine()) != null) {
data.append(mline);
}
} catch (IOException e) {
log(",e.getmessage()) } finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
log(",e.getmessage())
}
}
}
然后在webview中加载此数据
webview.loadData(data, "text/html", "UTF-8");
我正在考虑开始为android制作游戏。理论上,我是否可以创建一个html5画布和javascript游戏,在android手机上本地存储文件,然后用webview打开它们,让玩家玩游戏?我想从test.html导航到page1.html,我在test.html中使用
,因为两者都在同一目录中,但不起作用