Issue
I try to make a Bitmap from a view in Android in this way:
Bitmap myBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas myCanvas = new Canvas(myBitmap);
TextView myText = new TextView(myContext);
newText.setText("This is the text that its going to appear");
myText.layout(0, 0, width, height);
myText.draw(myCanvas);
and it works the same as for other views such as ImageView,....
But when i want to create a Bitmap from WebView, it is created but content does not appear. I also tried to use WebViewClient to detect when content has been already loaded, but the result is the same. Bellow is code i've used:
Bitmap myBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas myCanvas = new Canvas(myBitmap);
String mWebViewContent = "<html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>";
WebView lWebView = new WebView(myContext);
lWebView.layout(0, 0, width, height);
lWebView.setBackgroundColor(Color.GREEN);
lWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
view.draw(myCanvas);
//content height is 0 at this point
Log.d("info", "height: " + view.getContentHeight());
super.onPageFinished(view, url);
}
});
lWebView.loadData(mWebViewContent, "text/html", "utf-8");
Bitmap is created with green background but content does not apear. Any suggestions why the content is not drawn in the WebView or at what moment it can be ready for drawing? Thanks in advance for any help!!!
Solution
You should check out the answer at:
Rendering Android webview to bitmap, html5 javascript , callback issue
The key factor here is the postDelayed() call. There is a small lag between when the WebClient.onPageFinished() is called and when the page is actually rendered. It would be nice if there was a onPageRendered() method.
Answered By - iHearGeoff
Answer Checked By - Candace Johnson (JavaFixing Volunteer)