The webview control is often used in projects. When an HTML page is loaded, the database and cache folders are generated under the/data/application package directory, as shown in:
The requested URL record is saved in webviewcache. dB, and the URL content is saved in the webviewcache folder.
For ease of understanding, next we will simulate a case, define an HTML file, display an image in it, load it with webview, and then try again to read and display the image from the cache.
Step 1: Create an android project named webviewcache. The directory structure is as follows:
Step 2: Create an HTML file in the assetsdirectory and name it index.html.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Step 3: Modify the main. xml layout file, a webview control and a button (click to load the cached image). The Code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <webview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Id = "@ + ID/webview"/> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "center_horizontal" Android: text = "read images from cache" Android: Id = "@ + ID/button"/> </linearlayout>
Step 4: Modify the main core program webviewcachedemo.java. here I only wrote the index.html file. The button event is not written yet. The Code is as follows:
package com.ljq.activity;import java.io.File;import java.io.FileInputStream;import android.app.Activity;import android.app.Dialog;import android.app.AlertDialog.Builder;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.view.View;import android.webkit.WebView;import android.widget.Button;import android.widget.ImageView;public class WebViewActivity extends Activity { private WebView webView; private static final String url="file:///android_asset/index.html"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); webView=(WebView)findViewById(R.id.webView); webView.loadUrl(url); }}
Step 5: add the network access permission to the androidmainifest. xml file:
<uses-permission android:name="android.permission.INTERNET" />
The running effect is as follows:
In this case, we add a record to the cache. Table in webviewcache. DB, as shown in:
A 10d8d5cd file is added to the cache/webviewcache/directory, just like the filepath in cache. Table. We can conclude that this file is the image we pulled from the Internet:
To verify the conjecture, I added an Event Response to the button, that is, the dialog pop-up, which loads the cached image. The complete code is as follows:
Package COM. ljq. activity; import Java. io. file; import Java. io. fileinputstream; import android. app. activity; import android. app. dialog; import android. app. alertdialog. builder; import android. content. dialoginterface; import android. content. dialoginterface. onclicklistener; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. OS. bundle; import android. view. view; import android. webKit. webview; import android. widget. button; import android. widget. imageview; public class webviewactivity extends activity {private webview; Private Static final string url = "file: // android_asset/index.html"; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); webview = (webview) findviewbyid (R. id. webview); webview. loadurl (URL); // when you click a button, the dialog box "button = (button) findviewbyid (R. id. button); button. setonclicklistener (new view. onclicklistener () {public void onclick (view v) {imageview = new imageview (webviewactivity. this); imageview. setimagebitmap (getpicturefromcache (); Builder = new android. app. alertdialog. builder (webviewactivity. this); // set the icon builder in the dialog box. settitle ("viewing images from cache"); builder. setview (imageview); // exit button builder. setpositivebutton ("return", new onclicklistener () {public void onclick (dialoginterface dialog, int which) {// close alert dialog framework diert. cancel () ;}}); builder. create (). show () ;}}) ;}/ *** obtain the image from the cache ** @ return */private bitmap getpicturefromcache () {Bitmap bitmap = NULL; try {// write dead here. In actual development projects, you must flexibly use file = new file (getcachedir () + "/webviewcache/10d8d5cd "); fileinputstream instream = new fileinputstream (File); bitmap = bitmapfactory. decodestream (instream);} catch (exception e) {e. printstacktrace () ;}return bitmap ;}}
Step 6: run the project again and click the button. The effect is shown in:
This article from http://www.cnblogs.com/linjiqin/archive/2011/10/28/2227943.html