gpt4 book ai didi

android - 从android webview捕获图片

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:44:07 26 4
gpt4 key购买 nike

我想在 WebView 中保存通过 capturePicture 方法获取的图片,怎么办?

最佳答案

web = new WebView(this);

web.setPictureListener(new PictureListener(){

public void onNewPicture(WebView view, Picture picture) {
if(picture != null)
{
try
{
Bitmap bmp = pictureDrawable2Bitmap(new PictureDrawable(picture));
FileOutputStream out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
});

web.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView webview, String url) {
Picture picture = webview.capturePicture();

}
});


web.getSettings().setJavaScriptEnabled(true);

setContentView(web);

辅助函数

private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable){
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth()
,pictureDrawable.getIntrinsicHeight()
, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
return bitmap;
}

免责声明:我实际上并没有编译这个,不过它应该可以工作

关于android - 从android webview捕获图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7702565/

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