android webview 截屏

本文介绍如何通过WebView设置开启缓存,并获取缓存中的Bitmap图片,接着将该图片保存至SD卡的具体实现过程。

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

1.设置webview开起缓存:

mView.setDrawingCacheEnabled(true);

2.使用 Bitmap bitmap = mView.getDrawingCache();

获取一个Bitmap

3.保存到sdcard:

public  String savePic(Bitmap photo, String name, String path) {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  photo.compress(Bitmap.CompressFormat.PNG, 100, baos);
  byte[] byteArray = baos.toByteArray();
  String saveDir = Environment.getExternalStorageDirectory()
    .getAbsolutePath();
  File dir = new File(saveDir + "/" + path);
  if (!dir.exists()) {
   dir.mkdir();
  }
  File file = new File(dir, name + ".png");
  if (file.exists())
   file.delete();
  try {
   file.createNewFile();
   FileOutputStream fos;
   fos = new FileOutputStream(file);
   BufferedOutputStream bos = new BufferedOutputStream(fos);
   bos.write(byteArray);
   bos.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  //保存完后清除webview缓存的Bitmap对象,不清除,在加载其他页面,截屏时候,还是上个Bitmap
   mView.destroyDrawingCache();
   //回收Bitmap
   photo.recycle();
   photo=null;
  return file.getPath();
 }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值