gpt4 book ai didi

android - 如何打印整个 WebView(可滚动)?

转载 作者:行者123 更新时间:2023-11-30 00:22:12 28 4
gpt4 key购买 nike

我想在单击按钮时打印整个 WebView

我用的是西铁城打印机。我试过下面的方法。

private Bitmap CitizenWebPrint(WebView webView) {
Bitmap bitmapCitizen = null;
Picture capturePicture = webView.capturePicture();
int width = capturePicture.getWidth();
int height = capturePicture.getHeight();

try {
bitmapCitizen = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
} catch (OutOfMemoryError e) {
Toast.makeText(getApplicationContext(),"2",Toast.LENGTH_LONG).show();
}
capturePicture.draw(new Canvas(bitmapCitizen));

return bitmapCitizen;
}

但问题是,这只能打印 WebView 的可见部分。

How can I print the whole WebView?

最佳答案

绘制当前窗口的DecorView对象,然后绘制Bitmap对象。

你可以这样做。

在做之前,你可以这样做。

    WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webView.requestFocusFromTouch();
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("your url");

并检查版本。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkSdkVersion();
setContentView(R.layout.activity_webview_capture);
}

// check version
private void checkSdkVersion() {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
WebView.enableSlowWholeDocumentDraw();
}
}

在 list 中添加权限。

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

方式一

private void getScreenshot() {
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);
// save to the File
try {
String fileName = Environment.getExternalStorageDirectory().getPath() + "/webview_capture1.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
// Save
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromDraw.this, "Screenshot OK", Toast.LENGTH_LONG).show();
bitmap.recycle();
} catch (Exception e) {
e.getMessage();
}
}

方式 2 使用 webView.getDrawingCache();

private void getScreenshot() {

bitmap = webView.getDrawingCache();
try {
String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_capture2.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromDrawCache.this, "Screenshot OK", Toast.LENGTH_LONG).show();

} catch (Exception e) {
Log.e("TAG", e.getMessage());
}
}

@Override
protected void onDestroy() {
super.onDestroy();
//recycle
if(bitmap!=null) {
bitmap.recycle();
}
}

方法 3 和你的一样,在我的设备上运行良好。

private void getScreenshot() {
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_capture3.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromCapture.this, "Screenshot ok", Toast.LENGTH_LONG).show();
bitmap.recycle();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
}

关于android - 如何打印整个 WebView(可滚动)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46068761/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com