I'll give you an example that worked for me a long time ago, this WebView
showed an image of a site (Thanks to an answer from @Elenasys that I mixed code of it and others in S.O: p)
webview = (WebView) rootView.findViewById(R.id.loadImagePortada);
webview.getSettings().setAppCacheMaxSize(5 * 1024 * 1024); // 5MB
webview.getSettings().setAppCachePath(getActivity().getCacheDir().getAbsolutePath());
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setAppCacheEnabled(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
if ( !isNetworkAvailable() ) { // Cargando sin conexión
webview.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ONLY );
}
final AlertDialog dialogoAlerta = new AlertDialog.Builder(getActivity()).create();
progressBar = ProgressDialog.show(getActivity(), "Cargando portada", "Espere por favor...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
dialogoAlerta.setTitle("Error");
dialogoAlerta.setMessage(description + " Intente mas tarde por favor.");
dialogoAlerta.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
dialogoAlerta.show();
}
});
webview.loadUrl("http://mispruebas.eshost.com.ar/getimage.php");
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
The first time I opened the activity I loaded from the web, when I left that activity and later came back to enter and I did not download it, loaded from the cache.
It's a very functional example;)