android webView截圖的4種方法
android 在webView里面截圖大概有四種方式,具體內(nèi)容如下
1.獲取到DecorView然后將DecorView轉(zhuǎn)換成bitmap然后寫入到文件里面.
View view = getWindow().getDecorView();
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
Log.d(TAG,"bitmap--"+bitmap);
try {
String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_jietu.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
//壓縮bitmap到輸出流中
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromGetDecorView.this, "截屏成功", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}finally {
if(bitmap!=null) {
bitmap.recycle();
}
}
2.使用webViewpicture來實(shí)現(xiàn)該功能.(該方法被廢棄了因此不建議使用)
Picture picture = webView.capturePicture();
int width = picture.getWidth();
int height = picture.getHeight();
if (width > 0 && height > 0) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
picture.draw(canvas);
try {
String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_jietu.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
//壓縮bitmap到輸出流中
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromCapture.this, "截屏成功", Toast.LENGTH_LONG).show();
bitmap.recycle();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
3.使用webViewDraw來實(shí)現(xiàn).(該方法被廢棄了因此不建議使用)
float scale = webView.getScale();
int webViewHeight = (int) (webView.getContentHeight()*scale+0.5);
Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(),webViewHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
webView.draw(canvas);
try {
String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_jietu.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
//壓縮bitmap到輸出流中
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromDraw.this, "截屏成功", Toast.LENGTH_LONG).show();
bitmap.recycle();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
4.使用webViewDrawCache來實(shí)現(xiàn)(建議使用).
Bitmap bitmap = webView.getDrawingCache();
try {
String fileName = Environment.getExternalStorageDirectory().getPath() + "/webview_jietu.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
//壓縮bitmap到輸出流中
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
bitmap.recycle();
fos.close();
Toast.makeText(WebviewFromDrawCache.this, "截屏成功", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
} finally {
bitmap.recycle();
}
注意:
在android5.0及以上版本使用webView進(jìn)行截長圖時(shí),默認(rèn)是截取可是區(qū)域內(nèi)的內(nèi)容.因此需要在支撐窗體內(nèi)容之前加上如下方法.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
WebView.enableSlowWholeDocumentDraw();
}
setContentView(R.layout.activity_webview);
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android控件WebView實(shí)現(xiàn)完整截圖
- Android WebView實(shí)現(xiàn)網(wǎng)頁滾動(dòng)截圖
- Android使用WebView實(shí)現(xiàn)截圖分享功能
- Android 5.0及以上編程實(shí)現(xiàn)屏幕截圖功能的方法
- Android實(shí)現(xiàn)自動(dòng)截圖腳本
- Android中通過view方式獲取當(dāng)前Activity的屏幕截圖實(shí)現(xiàn)方法
- Android中如何獲取視頻文件的截圖、縮略圖
- Android模擬器中窗口截圖存成文件實(shí)現(xiàn)思路及代碼
- Android實(shí)現(xiàn)截圖和分享功能的代碼
- Android實(shí)現(xiàn)全屏截圖或長截屏功能
相關(guān)文章
Android App中實(shí)現(xiàn)簡單的刮刮卡抽獎(jiǎng)效果的實(shí)例詳解
這篇文章主要介紹了Android App中實(shí)現(xiàn)簡單的刮刮卡抽獎(jiǎng)效果的實(shí)例詳解,文中主要借助Bitmap的canvas.drawPath的api來實(shí)現(xiàn),需要的朋友可以參考下2016-03-03
Android webview 遇到android.os.FileUriExposedException錯(cuò)誤解決辦法
這篇文章主要介紹了Android webview 遇到android.os.FileUriExposedException錯(cuò)誤解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家遇到這樣的問題解決,需要的朋友可以參考下2017-10-10
Android實(shí)現(xiàn)多級(jí)樹形選擇列表
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多級(jí)樹形選擇列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Android Notification使用方法總結(jié)
這篇文章主要介紹了Android Notification使用方法總結(jié)的相關(guān)資料,這里提供了四種使用方法,需要的朋友可以參考下2017-09-09
Android StrictMode運(yùn)行流程(推薦)
strictmode是android在 API9后引入的檢測(cè)影響app運(yùn)行流暢性的一種機(jī)制。這篇文章給大家介紹了android strictmode運(yùn)行流程,需要的朋友參考下吧2018-01-01
Android Application存取公共數(shù)據(jù)的實(shí)例詳解
這篇文章主要介紹了Android Application存取公共數(shù)據(jù)的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
Android 提交或者上傳數(shù)據(jù)時(shí)的dialog彈框動(dòng)畫效果
我們?cè)谑褂弥Ц秾氈Ц兜臅r(shí)候會(huì)看到類似這種彈框動(dòng)畫效果,下面通過實(shí)例代碼給大家分享android 提交或者上傳數(shù)據(jù)時(shí)的彈框動(dòng)畫效果,感興趣的的朋友參考下2017-07-07
android使用RxJava實(shí)現(xiàn)預(yù)加載
這篇文章主要為大家詳細(xì)介紹了android使用RxJava實(shí)現(xiàn)預(yù)加載的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

