gpt4 book ai didi

java - 调用 onPageFinished() 时页面未完全加载

转载 作者:行者123 更新时间:2023-11-30 10:05:08 24 4
gpt4 key购买 nike

我想在页面加载完成后截取 Web View 的屏幕截图。所以我尝试在 onPageFinished() 中添加 getSnapShot(),但是当调用 getSnapShot() 时,页面没有加载。所以快照的图片是空白的。

我加载了一个本地 html 文件 url,所以我相信没有重定向。

我还尝试制作 MyWebView extends WebView,覆盖 onDraw(),添加回调监听器以在 处调用 getSnapShot() >onDraw() 过程。但还是不行。

webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);

// will get a blank picture
getSnapshot();
}
});

最佳答案

试试这段代码;

 webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Picture picture = view.capturePicture();
Bitmap b = Bitmap.createBitmap( picture.getWidth(),
picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas( b );

picture.draw( c );
FileOutputStream fos = null;
try {

fos = new FileOutputStream( "your_directory_screenshot.jpg" );
if ( fos != null )
{
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);

fos.close();
}
}
catch( Exception e )
{}
}
});

关于java - 调用 onPageFinished() 时页面未完全加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55470708/

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