我在根文件夹中保存了一个文件,并尝试在webview中打开它.
这是我保存的代码:
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); <---
并在应用程序内部,它给我一个文件未找到错误.
任何见解都有帮助.
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:///...
Then 打开URL时,意味着您应该将文件保存在assets目录下(比如test.html).现在假设你必须访问test.html文件,你需要这样写
loadURL("file:///android_asset/test.html');
假设异常没有被击中,路径是错误的.
file:///告诉浏览器查找/ name
openFileOutput(fileName)告诉应用程序在
您的网址应为"file:///"+ getFilesDir()+ File.separator + fileName