android webview 图片缓存,WebView图片缓存

这篇博客详细介绍了在Android项目中使用WebView控件加载HTML页面时,如何管理和操作数据库与缓存文件夹。通过初始化缓存数据库webviewCache,清理并获取图片路径,展示了对SQLite数据库的操作过程,以及从缓存中读取图片的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在项目中经常会使用到WebView控件,当加载html页面时,会在/data/data/应用package目录下生成database与cache两个文件夹如下图如示:private SQLiteDatabase database;

/**

* 初始化缓存数据库webviewCache

* 清空cache表中的数据

*/

private void InitDatabase(){

database = SQLiteDatabase.openDatabase(

getDatabasePath("webviewCache.db").getAbsolutePath(), null,

SQLiteDatabase.OPEN_READWRITE);

String sql = "delete from cache";

database.rawQuery(sql, new String[]{});

}

/**

* 从cache表中获取第一个图片路径

* 由于刚进入界面就清空了cache表

* 所以正常情况下,表中只存在一张缓存图片

* @return

*/

public String getBarName(){

String result = null;//

Cursor cursor = database.rawQuery("select * from cache",

new String[] {});

if(cursor.moveToNext()){

result =  cursor.getString(cursor.getColumnIndex("filepath"));

}

cursor.close();

database.close();

return result;

}

/**

* 从缓存获取图片

*

* @return

*/

private Bitmap getPictureFromCache(String str){

Bitmap bitmap=null;

try {

File file=new File(getCacheDir()+"/webviewCache/" + str);

FileInputStream inStream=new FileInputStream(file);

bitmap=BitmapFactory.decodeStream(inStream);

} catch (Exception e) {

e.printStackTrace();

}

return bitmap;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值