WebView加载本地存储的网页¶
加载app存储中的网页¶
我们把网页文件放到app的私有目录中。
路径为:/data/user/0/com.rustfisher.tutorial2020/files/sample.html
url要以file://
开头: file:///data/user/0/com.rustfisher.tutorial2020/files/sample.html
设置WebSettings
,允许读取文件等等。
String url = "";
File htmlFile = new File(getFilesDir(), "sample.html");
Log.d(TAG, htmlFile + "\nfile exits: " + htmlFile.exists());
url = "file://" + htmlFile.getAbsolutePath();
Log.d(TAG, "url: " + url);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowContentAccess(true);
webSettings.setDomStorageEnabled(true);
webView.loadUrl(url);
加载sd卡中的网页¶
与app内部存储类似,只需要修改路径。
例如在模拟器上的url为
file:///storage/emulated/0/sample.html