14

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.

dave.c
  • 10,910
  • 5
  • 39
  • 62
Tony P
  • 141
  • 1
  • 1
  • 3

6 Answers6

18
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

13

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');
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
siddhusingh
  • 1,832
  • 4
  • 25
  • 30
  • I want to navigate from test.html to page1.html, I am using `Page 1 ` in test.html because both are in same dir, But not working – Sarz Feb 03 '17 at 06:48
9

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

Phil Lello
  • 8,377
  • 2
  • 25
  • 34
  • I am thinking of getting started on making games for android. In theory, is it possible for me to create an html5 canvas and javascript game, store the files locally on the android phone and then open them up with webview allowing the player to play the game? – Jacob Nov 02 '11 at 19:47
0

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.

sh.e.salh
  • 508
  • 2
  • 5
  • 16
0

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

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

I've tested its solution and it works.

MosesK
  • 359
  • 3
  • 7