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">
<title>WebViewCacheDemo</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<body>
</body>
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 = "reading images from the 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 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 );
// The dialog box pops up when you click the button.
Button button = (Button) findViewById (R. id. button );
Button. setOnClickListener (new View. OnClickListener (){
Public void onClick (View v ){
ImageView imageView = new ImageView (WebViewActivity. this );
ImageView. setImageBitmap (getPictureFromCache ());
Builder builder = new android. app. AlertDialog. Builder (WebViewActivity. this );
// Set the icon in the dialog box
Builder. setTitle ("viewing images from cache ");
Builder. setView (imageView );
// Exit
Builder. setPositiveButton ("return", new OnClickListener (){
Public void onClick (DialogInterface dialog, int which ){
// Close the alert dialog framework
Dialog. cancel ();
}
});
Builder. create (). show ();
}
});
}
/**
* Retrieving images from the cache
*
* @ Return
*/
Private Bitmap getPictureFromCache (){
Bitmap bitmap = null;
Try {
// Write it down here and use it flexibly in actual development projects
File 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: