[英]load somefile.html file from internal storage to a webview of an app
[英]Kotlin load html from internal storage to webview
我想將內部存儲中的 html 頁面加載到 webview 中,但我找不到任何有關如何執行此操作的示例。 這是我可以做的:
webView.loadUrl("file:///android_asset/article_0028_0071.html")
這項工作,但我必須手動將頁面添加到資產。 然后我在內部存儲中得到了相同的文件,完整路徑是:/data/user/0/com.example.app/files/articles/article_0028_0071.html 所以我試過:
path = "/data/user/0/com.example.app/files/articles/article_0028_0071.html"
webView.loadUrl(path)
這給我沒有錯誤,但一個空白頁面。 還嘗試使用“file:///articles/article_0028_0071.html”,但結果相同,沒有錯誤的空白頁面。
所以我想知道我應該如何編寫從內部存儲訪問html的路徑?
您可以根據需要處理各種類型的 url,只需通過自定義的 WebViewClient 設置您的 webview
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.startWith("file:")) {
//do whatever you need
}
return false;
}else if(url.startWith("http"){....etc
您只需要在路徑之前添加 file:/// 。
path = "file:///data/user/0/com.example.app/files/articles/article_0028_0071.html"
webView.loadUrl(path)
這工作完美
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.