开发者

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 (IOExcep开发者_运维技巧tion 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.


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


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');


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


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.


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


Please refer to the youtube video https://youtu.be/n2KbDqoCv_Q?t=173

I've tested its solution and it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜