Loading local html file in webView android

后端 未结 3 2032
独厮守ぢ
独厮守ぢ 2020-12-24 02:01

I have to load an existing html file into a WebView that is located at this path in the file system:

/data/data/com.example.example/files/file.         


        
相关标签:
3条回答
  • 2020-12-24 02:50

    Try this, adding in a file:/// and doing it a little differently:

    WebView webView = (WebView)findViewById(R.id.webView1);
    webview.loadUrl("file:///data/data/com.example.example/files/file.html");  
    

    Instead of this, however, you could just put the file into your assets folder in the source code, and then do this:

    WebView webView = (WebView)findViewById(R.id.webView1);
    webview.loadUrl("file:///android_asset/file.html");
    
    0 讨论(0)
  • 2020-12-24 02:55

    The html file should be placed in the assets folder, which will belong in the root directory of your project.

    So move your file to in case of eclipse

    assets/index.html
    

    In an Android Studio project use this folder:

    /app/src/main/assets/index.html
    

    Now use

    WebView wv= (WebView)findViewById(R.id.webView1);
    wv.loadUrl("file:///android_asset/index.html");
    
    0 讨论(0)
  • 2020-12-24 03:07

    You need to implement a ContentProvider to map local files to uris as explained in this link how to display a local file into Android Webview

    or you just load any html page from Assets folder like below:

     WebView wv= (WebView)findViewById(R.id.webView1);
     wv.loadUrl("file:///android_asset/yourfile.html");
     wv.getSettings().setJavaScriptEnabled(true);
    
    0 讨论(0)
提交回复
热议问题