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(); }
