Load a saved html file into a webview
Similar to this question, I am trying to load an HTML file into a webview. This HTML file is not created at compile time, but at runtime. I load a webpage, and cache it into storage for later use, in case the user is not internet-connected and wishes to view the information on the page.
Here is how I write the html out:
FileOutputStream fos = openFileOutput("info.html", Context.MODE_PRIVATE);
URL url = new URL(surl);
InputStream is = url.openStrea开发者_C百科m();
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ( (len = is.read(buffer)) != -1 )
{
byteBuffer.write(buffer, 0, len );
}
String outputString = new String(byteBuffer.toByteArray());
fos.write(outputString.getBytes());
fos.close();
I am able to open and read the HTML in a similar fashion, but for some reason, trying to load the html into the webview as a string doesn't work. I figured it would be easier for the webview to open it as a file (since it is saved as a file anyway). I'm unsure of the proper way to load this file now. Here's a few methods I have tried:
view.loadUrl("file:///info.html");
view.loadUrl("info.html");
view.loadUrl("/info.html");
try
file:///data/data/info.html
Might be just one data, but I think the local files are stored in the data section of your app.
精彩评论