我正在使用URL向Webview中显示一个html页面,我想将该HTML文件从Webview缓存存储到设备的本地存储中。有可能,请帮忙。
答案 0 :(得分:0)
Webview缓存
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<style>
.exa-title{
font-style: italic;
margin-left:10px;
color: dimgray;
}
</style>
<body>
<img class="word-image"src="" alt="">
<span class="ex-title"></span><span class="ex-sentence"></span>
<input class="word-input" type="text" name="" autofocus>
<script>
window.addEventListener('load', init);
const vocabularyList = [
{
image: "https://media.istockphoto.com/photos/red-apple-with-leaf-isolated-on-white-background-picture-id185262648?k=6&m=185262648&s=612x612&w=0&h=u9rMspGGTOkgUSzZ6INtT_Ww4NpGz9zHMGRmIJJjBqQ=",
word: "apple",
example: "The red apple."
},
{
image: "https://upload.wikimedia.org/wikipedia/commons/8/88/Bright_red_tomato_and_cross_section02.jpg",
word: "tomato",
example: "The red tomato."
}
];
function init(){
//DOM variables
const wordImage = document.querySelector('word-image');
const ex_title = document.querySelector('ex-title');
const ex_sentence = document.querySelector('ex-sentence');
const wordInput = document.querySelector('word-input');
// Load the first object from the array
showCard(vocabularyList);
//Start matching on word input
wordInput.addEventListener('input', matchWords);
}
// Pick and show random word
function showCard(vocabularyList){
//Generate random array index
const randIndex = Math.floor( Math.random() * vocabularyList.length);
//Output the random word
wordImage.src = vocabularyList[randIndex].image;
}
function matchWords(){
if(wordInput.value === vocabularyList[randIndex].word){
// ex_title.innerHTML = "p. ej.";
ex_sentence.innerHTML = vocabularyList[randIndex].example;
}
}
</script>
</body>
</html>
检查Internet可用性并为清单文件添加权限
WebView webView = new WebView( context );
webView.getSettings().setAppCacheMaxSize( 6 * 1024 * 1024 ); // 6MB size
webView.getSettings().setAppCachePath( getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default
if ( !isNetworkAvailable() ) { // loading offline
webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
}
webView.loadUrl( "http://www.stackoverflow.com" );
互联网读写存储权限
private boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}