'Open Local Html File in Webview - Android
I have a saved a file in the root folder and am trying to open it in a webview.
This is my code for saving:
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...");
}
This is my code for recalling the file:
fileView.getSettings().setJavaScriptEnabled(true);
fileView.loadUrl("file:///" + name); <---
and inside the app it gives me a file not found error.
Any insight is helpful.
Solution 1:[1]
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;
}
}
try this
Solution 2:[2]
Actually when you open a URL using file:///...
Then it means that you should save the file under assets directory (say test.html ).
Now suppose you have to access test.html file, you need to write like this
loadURL("file:///android_asset/test.html');
Solution 3:[3]
The path is wrong, assuming the exceptions weren't hit.
file:/// tells the browser to look for /name
openFileOutput(fileName) tells the app to write in <application-files-directory>/fileName
Your url should be "file:///"+getFilesDir()+File.separator+fileName
Solution 4:[4]
For files that will be bundled with the application you can add an "asset" folder to your project by right clicking your app in the project explorer then select
New=> Folder=> Assets Folder.
Add the HTML file to your asset folder then load it by:
fileView.loadUrl("file:///android_asset/"+name);
same URL can be used in your HTML to link to other HTML or CSS files.
Solution 5:[5]
You can read your asset file first and then display it on webview via asd like this
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())
}
}
}
and then load this data in webview
webview.loadData(data, "text/html", "UTF-8");
Solution 6:[6]
Please refer to the youtube video https://youtu.be/n2KbDqoCv_Q?t=173
I've tested its solution and it works.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Sergey Glotov |
| Solution 3 | Phil Lello |
| Solution 4 | sh.e.salh |
| Solution 5 | abdevwarlock |
| Solution 6 | MosesK |
