Loading html file from local folder into webview

后端 未结 3 741
悲&欢浪女
悲&欢浪女 2020-12-23 18:26

I\'m new to Android development.

I want to load a html file into a webview.

Note that there are so many relevant questions on SO like this,

相关标签:
3条回答
  • 2020-12-23 19:08

    In Android 4.4 KitKat, a "Not allowed to load local resource: file:///.." is thrown. arise when loadURL and the only alternative I've found is "loadDataWithBaseURL".

    webView.loadDataWithBaseURL("file:///android_asset/demo/",
                                 tmpDocumentText,"text/html", "UTF-8", null);
    
    0 讨论(0)
  • 2020-12-23 19:18

    You can use:

    
       WebView webView = // ...
    
       webView.loadUrl("file:///myPath/myFile.html");
    

    In an Android application, files can be read from 3 types of locations:

    • Internal storage: Each app has its own, file names are relative to this location. URL takes form file:///myFolder/myFile.html

    • External storage: Needs permission and may not always be available. Get the root folder by calling Environment.getExternalStorageDirectory(). So, construct the URL using: String url = "file:///" + Environment.getExternalStorageDirectory().toString() + File.separator + "myFolder/myFile.html"

    • Assets: Stored in the apk. Read-only access. URL takes form file:///android_asset/myFolder/myFile.html (See also Loading an Android Resource into a WebView)

    0 讨论(0)
  • 2020-12-23 19:19

    WebView has loadData method http://developer.android.com/reference/android/webkit/WebView.html

    All you need to do is reading the file into String then feed it to WebView using loadData.

    0 讨论(0)
提交回复
热议问题