< html > < head > < title > WebViewCacheDemo < / title > < meta http - equiv = " keywords " content = " keyword 1 , keyword 2 , keyword 3 " > < meta http - equiv = " description " content = " this is my page " > < meta http - equiv = " content - type " content = " text / html ; charset = UTF - 8 " > < / head > < body > < img src = " http : / / img 04 . taobaocdn . com / imgextra / i 4 / 608825099 / T 2 nGXBXXpaXXXXXXXX _ ! ! 608825099 . jpg _ 310 x 310 . jpg " / > < / body > < / html > < ? 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 = " 从 缓存 读取 图片 " android : id = " @ + id / button " / > < / LinearLayout > 第 四 步 : 修改 主 核心 程序 WebViewCacheDemo . java , 这里 我 只 加载 了 index . html 文件 , 按钮 事件 暂时 没 写 , 代码 如下 : 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 ) ; } } 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 ) ; / / 点击 按钮 时 弹 出 对话 框 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 ) ; / / 设置 对话 框 的 图标 builder . setTitle ( " 从 缓存 查看 图片 " ) ; builder . setView ( imageView ) ; / / 退出 按钮 builder . setPositiveButton ( " 退 出 " , new OnClickListener ( ) { public void onClick ( DialogInterface dialog , int which ) { / / 关闭 alert 对话 框架 dialog . cancel ( ) ; } } ) ; builder . create ( ) . show ( ) ; } } ) ; } / * * * 从 缓存 获取 图片 * * @ return * / private Bitmap getPictureFromCache ( ) { Bitmap bitmap = null ; try { / / 这里 写 死 , 在 实际 开发 项目 中 要 灵活 使用 File file = new File ( getCacheDir ( ) + " / webviewCache / 10d 8d 5 cd " ) ; FileInputStream inStream = new FileInputStream ( file ) ; bitmap = BitmapFactory . decodeStream ( inStream ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } return bitmap ; } }" />
您当前的位置:首页 > 博客教程

android webview缓存_android webview缓存

时间:2023-11-22 11:39 阅读数:7128人阅读

(-__-)b

+▂+

Non-compliance ICP Filing

?▂?

快帆加速器部分文章、数据、图片来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知删除。邮箱:xxxxxxx@qq.com